-
Notifications
You must be signed in to change notification settings - Fork 248
feat(Http): Http service can make cross-site requests (get, post, put, e... #1026
feat(Http): Http service can make cross-site requests (get, post, put, e... #1026
Conversation
})); | ||
|
||
it('head should pass on withCredentials to backend', async((FakeBackend backend) { | ||
http.head('/url', withCredentials: true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbdeboer I'm not sure if all these tests are needed. They all test pretty much the same thing. I'm thinking of keeping just the first test. What are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a single test with
['get', 'put', 'delete', 'head', ...].forEach((method) {
Function.apply(...);
expect(backend.method).toEqual(method);
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vicb Some parameters are a bit different for those methods. Post and Put have data parameter mandatory, while others only require url. I thought about this approach but I'm not sure if there's enough value that those tests bring...
@@ -422,6 +422,7 @@ class Http { | |||
data, | |||
Map<String, dynamic> params, | |||
Map<String, dynamic> headers, | |||
withCredentials: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bool
@@ -1356,3 +1373,40 @@ class FakeFile implements File { | |||
Blob slice([int start, int end, String contentType]) => null; | |||
int get lastModified => new DateTime.now().millisecondsSinceEpoch; | |||
} | |||
|
|||
class FakeBackend extends Mock implements HttpBackend { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use MockHttpBackend?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbdeboer MockHttpBackend
won't tell me if the method was called with correct parameters. Is there a way to get that info from MockHttpBacked
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you use something like https://github.com/angular/angular.dart/blob/a5d5c2b17a64acadb7935bf734da7e92d3a4cf74/test/core_dom/http_spec.dart#L72
It should be extended to support withCrdentials, ie
backend.expect('GET', '/url', withCrdentials: true).respond('');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to @vicb's comment. You should extend the MockHttpBackend as well.
@vicb @jbdeboer I've refactored this to use MockHttpBackend (and added support for withCredentials to it). I noticed one thing - the positional optional parameters are not that nice to work with. I think it might be beneficial to use named optional parameters when setting up Instead of having
It'd be nicer and more readable to have
This way http backend configuration is a lot more specific and readable. This would also make HttpBackend, in a way, aligned with HttpRequest which also uses optional named parameters. Do you guys think it's worth refactoring that MockHttpBackend to use optional named args instead of optional positional args? |
@@ -102,6 +104,10 @@ class MockHttpExpectation { | |||
return JSON.encode(data) == JSON.encode(d); | |||
} | |||
|
|||
bool matchWithCredentials(withCredentials) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about bool matchWithCredentials(bool value) => withCredentials == value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
…, etc.) which use credentials (such as cookies or authorization headers). Closes dart-archive#945
if (!expectation.matchWithCredentials(withCredentials)) | ||
throw ['Expected $expectation with different withCredentials\n' | ||
'EXPECTED: ${prettyPrint(expectation.withCredentials)}\n' | ||
'GOT: ${prettyPrint(withCredentials)}']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would align strings here (continuation lines should be indented w/ at least 4 spaces).
Side question: why do we throw lists instead of throwing strings ?
👍 for named parameters |
MockHttpBackend takes one more parameter, withCredentials. This is used to mimic withCredentials parameter from real HttpRequest.
I reworked MockHttpBackend.call to take named parameters. Other than that, LGTM. This is in the presubmit queue now. |
…, etc.) which use credentials (such as cookies or authorization headers). Closes dart-archive#945 Closes dart-archive#1026
…, etc.) which use credentials (such as cookies or authorization headers). Closes dart-archive#945 Closes dart-archive#1026
...tc.) which use credentials (such as cookies or authorization headers).