Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: http request tracker clear (#100) #101

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,18 @@ function declareDefaultClientTests(options: ClientTestDeclarationOptions) {
expect(returnedNotifications).toEqual([]);

expect(listRequests).toHaveLength(1);
expect(listTracker.requests()).toHaveLength(1);

listTracker.clear();

response = await listNotifications(notification.userId);
expect(response.status).toBe(200);

returnedNotifications = (await response.json()) as Notification[];
expect(returnedNotifications).toEqual([]);

expect(listRequests).toHaveLength(1);
expect(listTracker.requests()).toHaveLength(0);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ class HttpRequestTracker<

bypass(): HttpRequestTracker<Schema, Method, Path, StatusCode> {
this.createResponseDeclaration = undefined;
return this;
}

clear(): HttpRequestTracker<Schema, Method, Path, StatusCode> {
this.restrictions = [];
this.interceptedRequests = [];
return this;
return this.bypass();
}

matchesRequest(request: HttpInterceptorRequest<Default<Schema[Path][Method]>>): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,23 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn
expect(tracker.matchesRequest(parsedRequest)).toBe(true);
});

it('should clear restrictions after bypassed', async () => {
it('should not match any request if cleared', async () => {
const tracker = new HttpRequestTracker<Schema, 'GET', '/users'>(interceptor, 'GET', '/users');

const request = new Request(baseURL);
const parsedRequest = await HttpInterceptorWorker.parseRawRequest<MethodSchema>(request);
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.bypass();
tracker.clear();
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker
.with((_request) => false)
.respond({
status: 200,
body: { success: true },
});
expect(tracker.matchesRequest(parsedRequest)).toBe(false);
tracker.respond({
status: 200,
body: { success: true },
});
expect(tracker.matchesRequest(parsedRequest)).toBe(true);

tracker.bypass();
tracker.clear();
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.respond({
Expand Down Expand Up @@ -242,6 +240,35 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn
expect(interceptedRequests[1].response).toEqual(secondResponse);
});

it('should clear the intercepted requests and responses after cleared', async () => {
const tracker = new HttpRequestTracker<Schema, 'GET', '/users'>(interceptor, 'GET', '/users').respond({
status: 200,
body: { success: true },
});

const firstRequest = new Request(baseURL);
const parsedFirstRequest = await HttpInterceptorWorker.parseRawRequest<MethodSchema>(firstRequest);

const firstResponseDeclaration = await tracker.applyResponseDeclaration(parsedFirstRequest);
const firstResponse = Response.json(firstResponseDeclaration.body, {
status: firstResponseDeclaration.status,
});
const parsedFirstResponse = await HttpInterceptorWorker.parseRawResponse<MethodSchema, 200>(firstResponse);

tracker.registerInterceptedRequest(parsedFirstRequest, parsedFirstResponse);

const interceptedRequests = tracker.requests();
expect(interceptedRequests).toHaveLength(1);

expect(interceptedRequests[0]).toEqual(firstRequest);
expect(interceptedRequests[0].response).toEqual(firstResponse);

tracker.clear();

expect(interceptedRequests).toHaveLength(1);
expect(tracker.requests()).toHaveLength(0);
});

it('should provide access to the raw intercepted requests and responses', async () => {
const tracker = new HttpRequestTracker<Schema, 'GET', '/users'>(interceptor, 'GET', '/users').respond({
status: 200,
Expand Down Expand Up @@ -465,5 +492,61 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn
}
});
});

it('should clear restrictions after cleared', async () => {
const tracker = new HttpRequestTracker<Schema, 'GET', '/users'>(interceptor, 'GET', '/users');

const request = new Request(baseURL);
const parsedRequest = await HttpInterceptorWorker.parseRawRequest<MethodSchema>(request);
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.clear();
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker
.with((_request) => false)
.respond({
status: 200,
body: { success: true },
});
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.clear();
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.respond({
status: 200,
body: { success: true },
});
expect(tracker.matchesRequest(parsedRequest)).toBe(true);
});

it('should not clear restrictions after bypassed', async () => {
const tracker = new HttpRequestTracker<Schema, 'GET', '/users'>(interceptor, 'GET', '/users');

const request = new Request(baseURL);
const parsedRequest = await HttpInterceptorWorker.parseRawRequest<MethodSchema>(request);
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.bypass();
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker
.with((_request) => false)
.respond({
status: 200,
body: { success: true },
});
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.bypass();
expect(tracker.matchesRequest(parsedRequest)).toBe(false);

tracker.respond({
status: 200,
body: { success: true },
});
expect(tracker.matchesRequest(parsedRequest)).toBe(false);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,27 @@ export interface HttpRequestTracker<
) => HttpRequestTracker<Schema, Method, Path, StatusCode>;

/**
* Clears any restrictions and declared responses and makes the tracker stop matching intercepted requests. The next
* tracker, created before this one, that matches the same method and path will be used if present. If not, the
* requests of the method and path will not be intercepted.
* Clears any declared responses, making the tracker stop matching intercepted requests. The next tracker, created
* before this one, that matches the same method and path will be used if present. If not, the requests of the method
* and path will not be intercepted.
*
* To make the tracker match requests again, register a new response with `tracker.respond()`.
*
* @see {@link https://github.com/diego-aquino/zimic#trackerbypass}
*/
bypass: () => HttpRequestTracker<Schema, Method, Path, StatusCode>;

/**
* Clears any declared responses, restrictions, and intercepted requests, making the tracker stop matching intercepted
* requests. The next tracker, created before this one, that matches the same method and path will be used if present.
* If not, the requests of the method and path will not be intercepted.
*
* To make the tracker match requests again, register a new response with `tracker.respond()`.
*
* @see {@link https://github.com/diego-aquino/zimic#trackerclear}
*/
clear: () => HttpRequestTracker<Schema, Method, Path, StatusCode>;

/**
* @returns The intercepted requests that matched this tracker, along with the responses returned to each of them.
* This is useful for testing that the correct requests were made by your application.
Expand Down
Loading