Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

feat(Http): Http service can make cross-site requests (get, post, put, e... #1026

Conversation

mvuksano
Copy link
Contributor

@mvuksano mvuksano commented May 9, 2014

...tc.) which use credentials (such as cookies or authorization headers).

}));

it('head should pass on withCredentials to backend', async((FakeBackend backend) {
http.head('/url', withCredentials: true);
Copy link
Contributor Author

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?

Copy link
Contributor

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);
});

Copy link
Contributor Author

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...

@mvuksano mvuksano added cla: yes and removed cla: no labels May 9, 2014
@@ -422,6 +422,7 @@ class Http {
data,
Map<String, dynamic> params,
Map<String, dynamic> headers,
withCredentials: false,
Copy link
Contributor

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use MockHttpBackend?

Copy link
Contributor Author

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?

Copy link
Contributor

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('');

Copy link
Contributor

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.

@mvuksano
Copy link
Contributor Author

@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 expect and when. In my opinon it would make tests more readable and would make expectations/whens a lot more explicit.

Instead of having

hb.when('GET', '/url1', [], [], true).respond(201, 'another', {});

It'd be nicer and more readable to have

hb.when('/url1', method: 'GET', data: [], headers: [], withCredentials: true).respond(201, 'another', {});

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) {
Copy link
Contributor

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

Copy link
Contributor Author

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)}'];
Copy link
Contributor

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 ?

@vicb
Copy link
Contributor

vicb commented May 22, 2014

👍 for named parameters

MockHttpBackend takes one more parameter, withCredentials. This is used to mimic withCredentials parameter from real HttpRequest.
mvuksano added a commit that referenced this pull request May 22, 2014
…, etc.) which use credentials (such as cookies or authorization headers).

Closes #945
Closes #1026
@jbdeboer
Copy link
Contributor

I reworked MockHttpBackend.call to take named parameters. Other than that, LGTM. This is in the presubmit queue now.

@mvuksano mvuksano closed this in 3ef9d8e May 22, 2014
mvuksano added a commit that referenced this pull request May 30, 2014
…, etc.) which use credentials (such as cookies or authorization headers).

Closes #945
Closes #1026
dsalsbury pushed a commit to dsalsbury/angular.dart that referenced this pull request Jul 16, 2014
…, etc.) which use credentials (such as cookies or authorization headers).

Closes dart-archive#945
Closes dart-archive#1026
dsalsbury pushed a commit to dsalsbury/angular.dart that referenced this pull request Jul 16, 2014
…, etc.) which use credentials (such as cookies or authorization headers).

Closes dart-archive#945
Closes dart-archive#1026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

4 participants