Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($httpBackend): Add missing expectHEAD() and expectOPTIONS() methods #7320

Closed
wants to merge 1 commit into from

Conversation

djsmith42
Copy link

This method was documented but not implemented.

With accompanying unit test.

CLA signed as [email protected].

@@ -1520,7 +1520,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {


function createShortMethods(prefix) {
angular.forEach(['GET', 'DELETE', 'JSONP'], function(method) {
angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD', 'OPTIONS'], function(method) {
Copy link
Contributor

Choose a reason for hiding this comment

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

OPTIONS is perfectly capable of containing a body, per the RFC. It's not clear that it's appropriate to be added here, because these short methods never include their body. It might be more suitable to created it with createShortMethodsWithData() if we have that in angular-mocks

Copy link
Author

Choose a reason for hiding this comment

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

Good catch. Perhaps I should move OPTIONS to the section below that handles PUT, POST, and PATCH?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I'm fairly sure we don't want these, yeah :( I can understand wanting to expect those requests, but they aren't going to be made with angular-mocks unless you ask for them, because no real XHR request actually gets made.

So unless your app manually tries to make an OPTIONS or HEAD request using the options/head methods (which aren't included, afaik), then this doesn't really accomplish anything

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, apparently the 'head' method is created as a short method. So maybe it's just the options one we don't want.

Copy link
Author

Choose a reason for hiding this comment

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

I added HEAD support because I am using $http({method: "HEAD", url: ...}) in my application, but when I went to unit test it with $httpBackend, I found that $httpBackend.expectHEAD was undefined. Since it was in the docs, I assumed this must be a bug: https://docs.angularjs.org/api/ngMock/service/$httpBackend#expectHEAD

Copy link
Contributor

Choose a reason for hiding this comment

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

Angular-mocks doesn't create a real request, so the browser isn't going to jump in and say "no, do an OPTIONS request first" as it occasionally will for real requests, case depending. But yes you're correct that we do want expectHEAD()

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, unless your app is actually sending an "OPTIONS" request on its own (which in my opinion, is a fairly rare occurrence), then you probably don't need it.

If you do want to add it, since it can contain data, it should be moved below to the second forEach, since it can have a body.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I'll nuke this PR and create a new one that only adds expectHEAD(). Stand by for another exciting installment!

Copy link
Contributor

Choose a reason for hiding this comment

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

Er, you don't need to nuke the PR, just commit --amend and force push ;)

Copy link
Author

Choose a reason for hiding this comment

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

Amended.

@caitp
Copy link
Contributor

caitp commented Apr 30, 2014

alright, it generally LGTM, lets just wait for travis (as a formality!)

@djsmith42
Copy link
Author

btw, I got some jshint failures while running the grunt test in code unrelated to my changes. Not sure if that will fail the Travis build.

expect(typeof hb.expectPATCH).toBe("function");
expect(typeof hb.expectDELETE).toBe("function");
expect(typeof hb.expectHEAD).toBe("function");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

The jshint error is here, it doesn't like your indentation. The expect(...) calls should be indented 2 spaces, rather than 4

Copy link
Contributor

Choose a reason for hiding this comment

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

After checking it out, it looks like the lines don't have a real 0x10 newline character between them, so they all appear on the same line.

Copy link
Author

Choose a reason for hiding this comment

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

Oh facepalm. Fixing now. :(

Copy link
Author

Choose a reason for hiding this comment

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

According to xxd, it has the proper 0x0a (a.k.a "\n") char at the end of each line.

This was documented but not implemented.

With accompanying unit test to ensure the $httpBackend.expect*()
methods exist.
@djsmith42
Copy link
Author

Amended to fix spacing.

@caitp
Copy link
Contributor

caitp commented Apr 30, 2014

Did I say 0x10, obviously that's supposed to be a decimal 10 ;) but yes, this is passing ci-checks now

@caitp caitp closed this in e1d6178 Apr 30, 2014
@caitp
Copy link
Contributor

caitp commented Apr 30, 2014

well, it's merged. github is being laggy showing it, but it's merged

@djsmith42
Copy link
Author

Oh my goodness. It's like a dream come true! My first AngularJS PR. Thanks for the guidance.

stickel pushed a commit to stickel/angular.js that referenced this pull request May 1, 2014
This was documented but not implemented.

With accompanying unit test to ensure the $httpBackend.expect*() methods exist.

Closes angular#7320
@djsmith42 djsmith42 deleted the http-backend-fix branch May 1, 2014 15:04
@djsmith42
Copy link
Author

Is there any chance this can get into 1.2.x? It didn't make it into 1.2.18 or 1.2.17. Is it planned for 1.2.19?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants