-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
headers.spec.js
115 lines (97 loc) · 2.86 KB
/
headers.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function testHeaderValue(headers, key, val) {
var found = false;
for (var k in headers) {
if (k.toLowerCase() === key.toLowerCase()) {
found = true;
expect(headers[k]).toEqual(val);
break;
}
}
if (!found) {
if (typeof val === 'undefined') {
expect(headers.hasOwnProperty(key)).toEqual(false);
} else {
throw new Error(key + ' was not found in headers');
}
}
}
describe('headers', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
it('should default common headers', function (done) {
var headers = axios.defaults.headers.common;
axios('/foo');
getAjaxRequest().then(function (request) {
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
expect(request.requestHeaders[key]).toEqual(headers[key]);
}
}
done();
});
});
it('should add extra headers for post', function (done) {
var headers = axios.defaults.headers.common;
axios.post('/foo', 'fizz=buzz');
getAjaxRequest().then(function (request) {
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
expect(request.requestHeaders[key]).toEqual(headers[key]);
}
}
done();
});
});
it('should reset headers by null or explicit undefined', function (done) {
axios.create({
headers: {
common: {
'x-header-a': 'a',
'x-header-b': 'b',
'x-header-c': 'c'
}
}
}).post('/foo', {fizz: 'buzz'}, {
headers: {
'Content-Type': null,
'x-header-a': null,
'x-header-b': undefined
}
});
getAjaxRequest().then(function (request) {
testHeaderValue(request.requestHeaders, 'Content-Type', null);
testHeaderValue(request.requestHeaders, 'x-header-a', null);
testHeaderValue(request.requestHeaders, 'x-header-b', undefined);
testHeaderValue(request.requestHeaders, 'x-header-c', 'c');
done();
});
});
it('should use application/json when posting an object', function (done) {
axios.post('/foo/bar', {
firstName: 'foo',
lastName: 'bar'
});
getAjaxRequest().then(function (request) {
testHeaderValue(request.requestHeaders, 'Content-Type', 'application/json');
done();
});
});
it('should remove content-type if data is empty', function (done) {
axios.post('/foo');
getAjaxRequest().then(function (request) {
testHeaderValue(request.requestHeaders, 'Content-Type', undefined);
done();
});
});
it('should preserve content-type if data is false', function (done) {
axios.post('/foo', false);
getAjaxRequest().then(function (request) {
testHeaderValue(request.requestHeaders, 'Content-Type', 'application/x-www-form-urlencoded');
done();
});
});
});