Skip to content

Commit

Permalink
feat: added chopper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhomlala committed Jul 8, 2024
1 parent 09c0ff8 commit d7b3d4d
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions packages/alice_chopper/test/alice_chopper_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,127 @@ void main() {
);
file.deleteSync();
});

test("should handle call with empty response", () async {
final mockClient = MockClient((request) async {
return http.Response(
'',
200,
headers: {'content-type': 'application/json'},
);
});

chopperClient = ChopperClient(
baseUrl: baseUrl,
client: mockClient,
interceptors: [aliceChopperAdapter],
);

await chopperClient.get(
Uri(
path: 'json',
),
headers: {'content-type': 'application/json'},
);
final requestMatcher = buildRequestMatcher(
checkTime: true,
headers: {"content-type": "application/json"},
contentType: "application/json",
queryParameters: {},
);

final responseMatcher = buildResponseMatcher(checkTime: true);

final callMatcher = buildCallMatcher(
checkId: true,
checkTime: true,
secured: true,
loading: true,
client: 'Chopper',
method: 'GET',
endpoint: '/json',
server: 'test.com',
uri: 'https://test.com/json',
duration: 0,
request: requestMatcher,
response: responseMatcher,
);

verify(() => aliceCore.addCall(any(that: callMatcher)));

final nextResponseMatcher = buildResponseMatcher(
status: 200,
size: 0,
checkTime: true,
body: '',
);

verify(
() => aliceCore.addResponse(any(that: nextResponseMatcher), any()),
);
});

test("should handle call with error", () async {
final mockClient = MockClient((request) async {
return http.Response(
'error',
404,
);
});

chopperClient = ChopperClient(
baseUrl: baseUrl,
client: mockClient,
interceptors: [aliceChopperAdapter],
);

await chopperClient.get(
Uri(
path: 'json',
),
);

final requestMatcher = buildRequestMatcher(
checkTime: true,
);

final responseMatcher = buildResponseMatcher(checkTime: true);

final callMatcher = buildCallMatcher(
checkId: true,
checkTime: true,
secured: true,
loading: true,
client: 'Chopper',
method: 'GET',
endpoint: '/json',
server: 'test.com',
uri: 'https://test.com/json',
duration: 0,
request: requestMatcher,
response: responseMatcher,
);

verify(() => aliceCore.addCall(any(that: callMatcher)));

final nextResponseMatcher = buildResponseMatcher(
status: 404,
size: 0,
checkTime: true,
);

verify(
() => aliceCore.addResponse(
any(that: nextResponseMatcher),
any(),
),
);

final errorMatcher = buildErrorMatcher(
checkError: true,
);

verify(() => aliceCore.addError(any(that: errorMatcher), any()));
});
});
}

0 comments on commit d7b3d4d

Please sign in to comment.