Skip to content

Commit

Permalink
fix: Create a new iron-request for every api call
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Grappendorf committed Aug 4, 2015
1 parent c273e7a commit 21fbbf0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
13 changes: 6 additions & 7 deletions grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@

<dom-module id="grapp-rest-resource">

<template>

<iron-request id="request"></iron-request>

</template>

<script type="text/javascript">(function() {
Polymer({
is: 'grapp-rest-resource',
Expand Down Expand Up @@ -152,6 +146,7 @@
return h;
},
_sendRequest: function(method, url, id, data, action) {
var request;
if (id == null) {
id = null;
}
Expand All @@ -161,13 +156,17 @@
if (action == null) {
action = null;
}
return this.$.request.send({
request = this._createRequestElement();
return request.send({
method: method,
url: this._prepareUrl(url || this.url, this.params, id, action),
headers: this._prepareHeaders(),
body: (data ? JSON.stringify(data) : void 0)
});
},
_createRequestElement: function() {
return document.createElement('iron-request');
},
_handleResponse: function(response, status, resolve, reject) {
var json;
json = (response && response.trim() !== '' ? JSON.parse(response) : {});
Expand Down
6 changes: 5 additions & 1 deletion src/grapp-rest-resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ Polymer
h

_sendRequest: (method, url, id = null, data = null, action = null) ->
@$.request.send
request = @_createRequestElement()
request.send
method: method
url: @_prepareUrl url || @url, @params, id, action
headers: @_prepareHeaders()
body: (if data then JSON.stringify data else undefined)

_createRequestElement: ->
document.createElement 'iron-request'

_handleResponse: (response, status, resolve, reject) ->
json = (if response && response.trim() != '' then JSON.parse response else {})
if status >= 200 && status <= 299
Expand Down
6 changes: 0 additions & 6 deletions src/grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

<dom-module id="grapp-rest-resource">

<template>

<iron-request id="request"></iron-request>

</template>

<!-- build:script inline dist -->
<script src="grapp-rest-resource.js"></script>
<!-- /build -->
Expand Down
10 changes: 6 additions & 4 deletions test/headers.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
<body>

<grapp-rest-resource id="element_with_headers" url="http://api.example.com"
headers='{"foo":123}'></grapp-rest-resource>
headers='{"foo":123}'></grapp-rest-resource>

<grapp-rest-resource id="element_with_token" url="http://api.example.com"
token="JWT"></grapp-rest-resource>
token="JWT"></grapp-rest-resource>

<script type="text/coffeescript">

describe 'with defined hedaers attribute ', ->

it 'sends the custom headers', ->
element = document.querySelector '#element_with_headers'
xhr_request = sinon.stub(element.$.request, 'send').returns new Promise(->)
xhr_request = sinon.stub().returns new Promise(->)
sinon.stub(element, '_createRequestElement').returns {send: xhr_request}
element.resource.index()
expect(xhr_request).to.have.been.calledWith sinon.match {headers: {Accept: 'application/json', foo: 123}}

describe 'with defined token attribute ', ->

it 'sends the an authorization header', ->
element = document.querySelector '#element_with_token'
xhr_request = sinon.stub(element.$.request, 'send').returns new Promise(->)
xhr_request = sinon.stub().returns new Promise(->)
sinon.stub(element, '_createRequestElement').returns {send: xhr_request}
element.resource.index()
expect(xhr_request).to.have.been.calledWith sinon.match {headers: {Authorization: 'JWT'}}

Expand Down

0 comments on commit 21fbbf0

Please sign in to comment.