-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let the browser figure out the Content-Type when possible. #80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,17 @@ | |
debounce-duration="150"></iron-ajax> | ||
</template> | ||
</test-fixture> | ||
<!-- note(rictic): | ||
This makes us dependent on a third-party server, but we need to be able | ||
to check what headers the browser actually sends on the wire. | ||
If necessary we can spin up our own httpbin server, as the code is open | ||
source. | ||
--> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests below are certainly important, but the dependency on a third party server in this case is very unhygienic. We don't really have a good strategy in place to tell WCT to spin up additional processes before running tests, either. What do you think the long term plan for solving this problem should look like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need only the most minimal of servers for these tests. ~20 lines of node should do it if we wanted to run the server ourselves as part of WCT. I don't think it would be unreasonable for WCT to have a little local httpbin, it could be useful beyond just iron-ajax. There are even a few projects where folks have started on implementing httpbin in node. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like it. Hopefully we can do that sooner rather than later. |
||
<test-fixture id="RealPost"> | ||
<template> | ||
<iron-ajax method="POST" url="http://httpbin.org/post"></iron-ajax> | ||
</template> | ||
</test-fixture> | ||
<script> | ||
suite('<iron-ajax>', function () { | ||
var responseHeaders = { | ||
|
@@ -313,6 +324,7 @@ | |
suite('when making POST requests', function() { | ||
setup(function() { | ||
ajax = fixture('TrivialPost'); | ||
realAjax = fixture('RealPost'); | ||
}); | ||
|
||
test('POSTs the value of the `body` attribute', function() { | ||
|
@@ -460,6 +472,68 @@ | |
|
||
}); | ||
|
||
suite('when making a POST over the wire', function() { | ||
test('FormData is handled correctly', function() { | ||
server.restore(); | ||
var requestBody = new FormData(); | ||
requestBody.append('a', 'foo'); | ||
requestBody.append('b', 'bar'); | ||
|
||
var ajax = fixture('RealPost'); | ||
ajax.body = requestBody; | ||
return ajax.generateRequest().completes.then(function() { | ||
expect(ajax.lastResponse.headers['Content-Type']).to.match( | ||
/^multipart\/form-data; boundary=.*$/); | ||
|
||
expect(ajax.lastResponse.form.a).to.be.equal('foo'); | ||
expect(ajax.lastResponse.form.b).to.be.equal('bar'); | ||
}); | ||
}); | ||
|
||
test('json is handled correctly', function() { | ||
server.restore(); | ||
var ajax = fixture('RealPost'); | ||
ajax.body = JSON.stringify({a: 'foo', b: 'bar'}); | ||
ajax.contentType = 'application/json'; | ||
return ajax.generateRequest().completes.then(function() { | ||
expect(ajax.lastResponse.headers['Content-Type']).to.match( | ||
/^application\/json(;.*)?$/); | ||
expect(ajax.lastResponse.json.a).to.be.equal('foo'); | ||
expect(ajax.lastResponse.json.b).to.be.equal('bar'); | ||
}); | ||
}); | ||
|
||
test('urlencoded data is handled correctly', function() { | ||
server.restore(); | ||
var ajax = fixture('RealPost'); | ||
ajax.body = 'a=foo&b=bar'; | ||
return ajax.generateRequest().completes.then(function() { | ||
expect(ajax.lastResponse.headers['Content-Type']).to.match( | ||
/^application\/x-www-form-urlencoded(;.*)?$/); | ||
|
||
expect(ajax.lastResponse.form.a).to.be.equal('foo'); | ||
expect(ajax.lastResponse.form.b).to.be.equal('bar'); | ||
}); | ||
}); | ||
|
||
test('xml is handled correctly', function() { | ||
server.restore(); | ||
var ajax = fixture('RealPost'); | ||
|
||
var xmlDoc = document.implementation.createDocument( | ||
null, "foo", null); | ||
var node = xmlDoc.createElement("bar"); | ||
node.setAttribute("name" , "baz"); | ||
xmlDoc.documentElement.appendChild(node); | ||
ajax.body = xmlDoc; | ||
return ajax.generateRequest().completes.then(function() { | ||
expect(ajax.lastResponse.headers['Content-Type']).to.match( | ||
/^application\/xml(;.*)?$/); | ||
expect(ajax.lastResponse.data).to.match( | ||
/<foo\s*><bar\s+name="baz"\s*\/><\/foo\s*>/); | ||
}); | ||
}); | ||
}); | ||
}); | ||
</script> | ||
|
||
|
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.
There is a (potentially undesired) side-effect of making this
null
by default: any observer that depends on this value will be called during initialization of the element. For example,_requestOptionsChanged
will be called automatically when aniron-ajax
instance is created, becausenull
is assigned tothis.contentType
. See: https://github.com/PolymerElements/iron-ajax/blob/content-type/iron-ajax.html#L267-L270In the case of
iron-ajax
, I don't think this will cause a bug immediately, but we have generally avoided defaultnull
unless it's really necessary.Does the value have to be
null
by default, or will it suffice for it to default toundefined
?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.
That makes sense, but it looks like it prevents the
_requestOptionsChanged
observer from firing, because it's waiting for all arguments to be defined. As a result, the tests forauto
fail.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.
Ah, yes, that's true. I guess
null
is the best alternative, then.