question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow handling preflight requests automatically in RequestMock

See original GitHub issue

What is your Scenario?

I’m trying to intercept the response from a certain public API and return custom response, but even with recommended headers to allow all I keep getting CORS issues.

What is the Current behavior?

I’m getting "RequestMock: CORS validation failed for a request specified as { url: “Url I use” }

What is the Expected behavior?

I should not get any CORS issues while using ‘access-control-allow-origin’: ‘*’ response headers

What is your public website URL? (or attach your complete example)

https://user.repsly.com

What is your TestCafe test code?

https://user.repsly.com

Your complete configuration file

import { RequestMock } from ‘testcafe’;

const mockExample = RequestMock().onRequestTo(‘https://services.repsly.com/LocalizationService/Dictionary/GetDefaults’) .respond({ response: ‘success’ }, 200, { ‘access-control-allow-origin’: ‘*’ })

fixtureTestcafe RequestMock issue .beforeEach(async t => { await t .maximizeWindow() });

test.requestHooks(mockExample)(‘DOESNT WORK - CORS origin error’, async t => { await t.navigateTo(‘https://user.repsly.com’); });

Your complete test report

{ “startTime”: “2022-09-28T12:27:32.951Z”, “endTime”: “2022-09-28T12:27:50.714Z”, “userAgents”: [ “Chrome 105.0.0.0 / Windows 10” ], “passed”: 1, “total”: 1, “skipped”: 0, “fixtures”: [ { “name”: “Testcafe RequestMock issue”, “path”: “E:\Projekti\TestCafeRepsly\testcafe\templates\QAB-12515-RequestMockCorsIssue.spec.ts”, “meta”: {}, “tests”: [ { “name”: “DOESNT WORK - CORS origin error”, “meta”: {}, “errs”: [], “durationMs”: 17719, “unstable”: false, “screenshotPath”: null, “skipped”: false } ] } ], “warnings”: [ “The "src" and "browsers" options from the configuration file will be ignored.”, “RequestMock: CORS validation failed for a request specified as { url: "https://services.repsly.com/LocalizationService/Dictionary/GetDefaults\” }" ] }

Screenshots

No need, everything is in the console.

Steps to Reproduce

  1. Just run the test example provided here and observe the results

TestCafe version

2.0.1

Node.js version

16.15.0

Command-line arguments

testcafe chrome pathToTest -r spec

Browser name(s) and version(s)

chrome 105

Platform(s) and version(s)

Windows 10

Other

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
miherlosevcommented, Oct 5, 2022

Is there any example how to use and chain nock library within testcafe block structure? I liked that I could chain test block like: “test.requestHooks(mockExample)”; is there equivalent using nock?

No, you can chain only TestСafe’s own methods. The correct way is to configure nock in the fixture.before hook or before the whole test run.

0reactions
gforepslycommented, Oct 3, 2022

Thx @miherlosev for suggestion. Is there any example how to use and chain nock library within testcafe block structure? I liked that I could chain test block like: “test.requestHooks(mockExample)”; is there equivalent using nock? Also, ALL of our requests/responses are the same as the public example I provided, so we will use nock once we figure out how to properly use and chain it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 4. Handling preflight requests - CORS in Action
The concept of a preflight was introduced to allow cross-origin requests to be made without breaking existing servers that depend on the browser's...
Read more >
Response headers not set when using RequestMock to mock ...
Request can be successfully mocked without any CORS issue, as I tried to set 'Access-Control-Allow-Origin' = '*' in custom headers.
Read more >
Automatic handling of CORS preflight OPTIONS requests
Handle preflight OPTIONS requests automatically with Mockoon. ... Enable the **Automatically handle OPTIONS pre-flight requests ** by ticking the checkbox:.
Read more >
End-to-End Testing with TestCafe Book Excerpt: Intercepting ...
The preflight request verifies if the originating server where the request is coming from is allowed to make a cross-domain request. If it...
Read more >
Preflight request - MDN Web Docs Glossary: Definitions of ...
A preflight request is automatically issued by a browser and in ... if it would allow a DELETE request, before sending a DELETE...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found