Skip to content

Commit

Permalink
test(#zimic): extend path parameter tests (#360)
Browse files Browse the repository at this point in the history
### Tests
- [#zimic] Added more test cases for interceptor workers, considering
paths starting and not starting with `/`.
  • Loading branch information
diego-aquino authored Aug 25, 2024
1 parent f24d8e4 commit a29f656
Showing 1 changed file with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,38 @@ export function declareMethodHttpInterceptorWorkerTests(options: SharedHttpInter
const param3 = '3';

it.each([
{
use: ':param',
fetch: `${param1}`,
params: { param: param1 },
},
{
use: ':param',
fetch: `${param2}`,
params: { param: param2 },
},
{
use: `${param1}`,
fetch: `${param1}`,
params: {},
},

{
use: ':param',
fetch: `/${param1}`,
params: { param: param1 },
},
{
use: ':param',
fetch: `/${param2}`,
params: { param: param2 },
},
{
use: `${param1}`,
fetch: `/${param1}`,
params: {},
},

{
use: '/:param',
fetch: `/${param1}`,
Expand All @@ -147,6 +179,22 @@ export function declareMethodHttpInterceptorWorkerTests(options: SharedHttpInter
params: {},
},

{
use: '/:param',
fetch: `${param1}`,
params: { param: param1 },
},
{
use: '/:param',
fetch: `${param2}`,
params: { param: param2 },
},
{
use: `/${param1}`,
fetch: `${param1}`,
params: {},
},

{
use: '/other/path/:param',
fetch: `/other/path/${param1}`,
Expand Down Expand Up @@ -242,16 +290,16 @@ export function declareMethodHttpInterceptorWorkerTests(options: SharedHttpInter
fetch: `/${param1}/path/${param2}/${param3}/path`,
params: {},
},
])(`should intercept ${method} requests with matching dynamic paths (use $use; fetch $fetch)`, async (paths) => {
const url = joinURL(baseURL, paths.use);
])(`should intercept ${method} requests with matching dynamic paths (use $use; fetch $fetch)`, async (path) => {
const url = joinURL(baseURL, path.use);

await usingHttpInterceptorWorker(workerOptions, async (worker) => {
const interceptor = createDefaultHttpInterceptor();
await promiseIfRemote(worker.use(interceptor.client(), method, url, spiedRequestHandler), worker);

expect(spiedRequestHandler).not.toHaveBeenCalled();

const urlExpectedToSucceed = joinURL(baseURL, paths.fetch);
const urlExpectedToSucceed = joinURL(baseURL, path.fetch);
const response = await fetch(urlExpectedToSucceed, { method });

expect(spiedRequestHandler).toHaveBeenCalledTimes(numberOfRequestsIncludingPreflight);
Expand All @@ -262,7 +310,7 @@ export function declareMethodHttpInterceptorWorkerTests(options: SharedHttpInter

const urlRegex = createRegexFromURL(url);
const parsedRequest = await HttpInterceptorWorker.parseRawRequest(handlerContext.request, { urlRegex });
expect(parsedRequest.pathParams).toEqual(paths.params);
expect(parsedRequest.pathParams).toEqual(path.params);

expect(response.status).toBe(200);
await expectMatchedBodyIfNotHead(response);
Expand Down Expand Up @@ -418,16 +466,16 @@ export function declareMethodHttpInterceptorWorkerTests(options: SharedHttpInter
},
])(
`should not intercept ${method} requests with non-matching dynamic paths (use $use; fetch $fetch)`,
async (paths) => {
const url = joinURL(baseURL, paths.use);
async (path) => {
const url = joinURL(baseURL, path.use);

await usingHttpInterceptorWorker(workerOptions, async (worker) => {
const interceptor = createDefaultHttpInterceptor();
await promiseIfRemote(worker.use(interceptor.client(), method, url, spiedRequestHandler), worker);

expect(spiedRequestHandler).not.toHaveBeenCalled();

const urlExpectedToFail = joinURL(baseURL, paths.fetch);
const urlExpectedToFail = joinURL(baseURL, path.fetch);

const fetchPromise = fetchWithTimeout(urlExpectedToFail, { method, timeout: 200 });
await expectFetchErrorOrPreflightResponse(fetchPromise, {
Expand Down

0 comments on commit a29f656

Please sign in to comment.