How to test equality between http Request objects

90 Views Asked by At

I want to test equality between these two request objects but I can not figure out how to implement the hash and equality operator on a third-party package for example http package. How can I use equatable package on the Response object of http package?

import 'package:http/http.dart' as http;
test('equality test', () async {
        final uri = baseUri.replace(path: 'account/login');
        final request = http.Request(RequestMethod.post, uri);
        final request2 = http.Request(RequestMethod.post, uri);
        expect(request, equals(request2));
});

The output is

Expected: Request:<POST https://test.webapi.odinpool.com:1989/account/login>
  Actual: Request:<POST https://test.webapi.odinpool.com:1989/account/login>

Which it doesn't complete successfully. I want to test the remote server API to call the correct API endpoint when creating the request object and test the method with verify.called(1) method but it seems the test package can not test equality between http.Request objects.

1

There are 1 best solutions below

0
On

Thanks to @Richard Heap comment I came up with a solution:

class RequestX extends http.Request with EquatableMixin{
  RequestX(super.method, super.url);

  @override
  List<Object?> get props => [super.headers, super.method, super.url, super.body];

}

This RequestX class compared two objects successfully.