Skip to content

Commit

Permalink
feat: Add a 'token' attribute for passing an authorization header wit…
Browse files Browse the repository at this point in the history
…h the HTTP requests
  • Loading branch information
Dirk Grappendorf committed Mar 17, 2015
1 parent 3ba72f1 commit 86dd824
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ Attributes

Use this URL instead of the default one for "PUT /resources/:id/action" requests.

* **headers**

- *type:* object

Set additional headers on the request.

* ** token**

- *type:* string

Set an authorization header with the specified token text.


Events
------
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"devDependencies": {
"coffee-script": "latest",
"jquery": "latest",
"web-component-tester": "Polymer/web-component-tester#latest"
"web-component-tester": "Polymer/web-component-tester#2.2.6"
}
}
5 changes: 4 additions & 1 deletion grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link href="../core-ajax/core-xhr.html" rel="import">

<polymer-element name="grapp-rest-resource"
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl headers">
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl headers token">

<template>

Expand Down Expand Up @@ -199,6 +199,9 @@
val = _ref[key];
h[key] = val;
}
if (this.token) {
h['Authorization'] = this.token;
}
return h;
}
});
Expand Down
1 change: 1 addition & 0 deletions src/grapp-rest-resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ Polymer 'grapp-rest-resource',
h = {'Accept': 'application/json'}
for key, val of @headers
h[key] = val
h['Authorization'] = @token if @token
h
2 changes: 1 addition & 1 deletion src/grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<link href="../core-ajax/core-xhr.html" rel="import">

<polymer-element name="grapp-rest-resource"
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl headers">
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl headers token">

<template>

Expand Down
2 changes: 1 addition & 1 deletion test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</head>
<body>

<grapp-rest-resource id="element" url="data.json"></grapp-rest-resource>
<grapp-rest-resource id="element" url="data.json" headers='{"foo":123}'></grapp-rest-resource>

<grapp-rest-resource id="element_with_special_url" url="data.json"
indexUrl="special_data.json"></grapp-rest-resource>
Expand Down
46 changes: 46 additions & 0 deletions test/headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!doctype html>
<html>

<head>
<meta charset="UTF-8">
<title>grapp-rest-resource headers tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

<script src="../lib/web-component-tester/browser.js"></script>
<script src="../lib/coffee-script/extras/coffee-script.js"></script>

<link href="../grapp-rest-resource.html" rel="import">
</head>
<body>

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

<grapp-rest-resource id="element_with_token" url="http://api.example.com"
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.$.xhr, 'request'
element.resource.index()
expect(xhr_request).to.have.been.calledWith sinon.match {headers: {foo: 123}}

describe 'with defined token attribute ', ->

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

</script>

<script>
</script>

</body>
</html>
3 changes: 2 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

<script>
WCT.loadSuites([
'basic.html'
'basic.html',
'headers.html'
]);
</script>

Expand Down

0 comments on commit 86dd824

Please sign in to comment.