Skip to content

Commit a81df6c

Browse files
Merge pull request js-cookie#40 from js-cookie/integration-api
Make it possible to test the encoding from server-side
2 parents 5266429 + 2fb8854 commit a81df6c

File tree

8 files changed

+503
-218
lines changed

8 files changed

+503
-218
lines changed

CONTRIBUTING.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
##Issues
1+
## Issues
22

33
- Report issues or feature requests on [GitHub Issues](https://github.com/js-cookie/js-cookie/issues).
44
- If reporting a bug, please add a [simplified example](http://sscce.org/).
55

6-
##Pull requests
6+
## Pull requests
77
- Create a new topic branch for every separate change you make.
88
- Create a test case if you are fixing a bug or implementing an important feature.
99
- Make sure the build runs successfully.
1010

1111
## Development
1212

13-
###Tools
13+
### Tools
1414
We use the following tools for development:
1515

1616
- [Qunit](http://qunitjs.com/) for tests.
1717
- [NodeJS](http://nodejs.org/download/) required to run grunt.
1818
- [Grunt](http://gruntjs.com/getting-started) for task management.
1919

20-
###Getting started
20+
### Getting started
2121
Install [NodeJS](http://nodejs.org/).
2222
Install globally grunt-cli using the following command:
2323

@@ -35,7 +35,7 @@ You should see a green message in the console:
3535

3636
Done, without errors.
3737

38-
###Tests
38+
### Tests
3939
You can also run the tests in the browser.
4040
Start a test server from the project root:
4141

@@ -45,7 +45,29 @@ This will automatically open the test suite at http://127.0.0.1:10000 in the def
4545

4646
_Note: we recommend cleaning all the browser cookies before running the tests, that can avoid false positive failures._
4747

48-
###Automatic build
48+
### Automatic build
4949
You can build automatically after a file change using the following command:
5050

5151
$ grunt watch
52+
53+
## Integration with server-side
54+
55+
js-cookie allows integrating the encoding test suite with solutions written in other server-side languages. To integrate successfully, the server-side solution need to execute the `test/encoding.html` file in it's integration testing routine with a web automation tool, like [Selenium](http://www.seleniumhq.org/). js-cookie test suite exposes an API to make this happen.
56+
57+
### ?integration_baseurl
58+
59+
Specify the base url to pass the cookies into the server through a query string. If `integration_baseurl` query is not present, then js-cookie will assume there's no server.
60+
61+
### window.global_test_results
62+
63+
After the test suite has finished, js-cookie exposes the global `window.global_test_results` property containing an Object Literal that represents the [QUnit's details](http://api.qunitjs.com/QUnit.done/). js-cookie also adds an additional property representing an Array containing the tests data.
64+
65+
### Handling requests
66+
67+
When js-cookie encoding tests are executed, it will request a url in the server through an iframe representing each test being run. js-cookie expects the server to handle the input and return the proper `Set-Cookie` headers in the response. js-cookie will then read the response and verify if the encoding is consistent with js-cookie default encoding mechanism
68+
69+
js-cookie will send some requests to the server from the baseurl in the format `/encoding?name=<cookie>`, where `<cookie>` represents the cookie-name to be read from the request.
70+
71+
The server should handle those requests, internally parsing the cookie from the request and writing it again. It must set an `application/json` content type containing an object literal in the content body with `name` and `value` keys, each representing the cookie-name and cookie-value decoded by the server-side implementation.
72+
73+
If the server fails to respond with this specification in any request, the related QUnit test will fail. This is to make sure the server-side implementation will always be in sync with js-cookie encoding tests for maximum compatibility.

Gruntfile.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,38 @@
33

44
module.exports = function (grunt) {
55

6+
function encodingMiddleware(request, response, next) {
7+
var url = require('url').parse(request.url, true, true);
8+
var query = url.query;
9+
var pathname = url.pathname;
10+
11+
if (pathname !== '/encoding') {
12+
next();
13+
return;
14+
}
15+
16+
var cookieName = query.name;
17+
var cookieValue = query.value;
18+
19+
response.setHeader('content-type', 'application/json');
20+
response.end(JSON.stringify({
21+
name: cookieName,
22+
value: cookieValue
23+
}));
24+
}
25+
626
grunt.initConfig({
727
pkg: grunt.file.readJSON('package.json'),
828
qunit: {
929
all: {
1030
options: {
11-
httpBase: 'http://127.0.0.1:9998'
12-
},
13-
src: ['test/index.html', 'test/encoding.html', 'test/amd.html']
14-
}
31+
urls: [
32+
'http://127.0.0.1:9998/',
33+
'http://127.0.0.1:9998/amd.html',
34+
'http://127.0.0.1:9998/encoding.html?integration_baseurl=http://127.0.0.1:9998/'
35+
]
36+
}
37+
},
1538
},
1639
nodeunit: {
1740
all: 'test/node.js'
@@ -76,15 +99,19 @@ module.exports = function (grunt) {
7699
}
77100
},
78101
connect: {
79-
'build-sauce': {
102+
'build-qunit': {
80103
options: {
81-
port: 9999,
82-
base: ['.', 'test']
104+
port: 9998,
105+
base: ['.', 'test'],
106+
middleware: function (connect, options, middlewares) {
107+
middlewares.unshift(encodingMiddleware);
108+
return middlewares;
109+
}
83110
}
84111
},
85-
'build-qunit': {
112+
'build-sauce': {
86113
options: {
87-
port: 9998,
114+
port: 9999,
88115
base: ['.', 'test']
89116
}
90117
},
@@ -94,7 +121,11 @@ module.exports = function (grunt) {
94121
base: ['.', 'test'],
95122
open: 'http://127.0.0.1:10000',
96123
keepalive: true,
97-
livereload: true
124+
livereload: true,
125+
middleware: function (connect, options, middlewares) {
126+
middlewares.unshift(encodingMiddleware);
127+
return middlewares;
128+
}
98129
}
99130
}
100131
},

bower.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,5 @@
44
"main": [
55
"src/js.cookie.js"
66
],
7-
"ignore": [
8-
"test",
9-
".*",
10-
"*.json",
11-
"*.md",
12-
"*.txt",
13-
"Gruntfile.js"
14-
]
7+
"ignore": []
158
}

test/.jshintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"extends": "../.jshintrc",
88
"globals": {
99
"require": true,
10-
"unescape": true
10+
"unescape": true,
11+
"lifecycle": true,
12+
"using": true,
13+
"addEvent": true
1114
}
1215
}

test/encoding.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
</head>
1212
<body>
1313
<div id="qunit"></div>
14-
<div id="qunit-fixture"></div>
14+
<div id="qunit-fixture">
15+
<iframe id="request_target"></iframe>
16+
</div>
1517
</body>
1618
</html>

0 commit comments

Comments
 (0)