forked from axios/axios
/
transform.spec.js
177 lines (147 loc) · 4.09 KB
/
transform.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
describe('transform', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
it('should transform JSON to string', function (done) {
var data = {
foo: 'bar'
};
axios.post('/foo', data);
getAjaxRequest().then(function (request) {
expect(request.params).toEqual('{"foo":"bar"}');
done();
});
});
it('should transform string to JSON', function (done) {
var response;
axios('/foo').then(function (data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: '{"foo": "bar"}'
});
setTimeout(function () {
expect(typeof response.data).toEqual('object');
expect(response.data.foo).toEqual('bar');
done();
}, 100);
});
});
it('should throw a SyntaxError if JSON parsing failed and responseType is "json" if silentJSONParsing is false',
function (done) {
var thrown;
axios({
url: '/foo',
responseType: 'json',
transitional: {silentJSONParsing: false}
}).then(function () {
done(new Error('should fail'));
}, function (err) {
thrown = err;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: '{foo": "bar"}' // JSON SyntaxError
});
setTimeout(function () {
expect(thrown).toBeTruthy();
expect(thrown.name).toContain('SyntaxError');
done();
}, 100);
});
}
);
it('should send data as JSON if request content-type is application/json', function (done) {
var response;
axios.post('/foo', 123, {headers: {'Content-Type': 'application/json'}}).then(function (_response) {
response = _response;
}, function (err) {
done(err);
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: ''
});
setTimeout(function () {
expect(response).toBeTruthy();
expect(request.requestHeaders['Content-Type']).toBe('application/json');
expect(JSON.parse(request.params)).toBe(123);
done();
}, 100);
});
});
it('should not assume JSON if responseType is not `json`', function (done) {
var response;
axios.get('/foo', {
responseType: 'text',
transitional: {
forcedJSONParsing: false
}
}).then(function (_response) {
response = _response;
}, function (err) {
done(err);
});
var rawData = '{"x":1}';
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
responseText: rawData
});
setTimeout(function () {
expect(response).toBeTruthy();
expect(response.data).toBe(rawData);
done();
}, 100);
});
});
it('should override default transform', function (done) {
var data = {
foo: 'bar'
};
axios.post('/foo', data, {
transformRequest: function (data) {
return data;
}
});
getAjaxRequest().then(function (request) {
expect(typeof request.params).toEqual('object');
done();
});
});
it('should allow an Array of transformers', function (done) {
var data = {
foo: 'bar'
};
axios.post('/foo', data, {
transformRequest: axios.defaults.transformRequest.concat(
function (data) {
return data.replace('bar', 'baz');
}
)
});
getAjaxRequest().then(function (request) {
expect(request.params).toEqual('{"foo":"baz"}');
done();
});
});
it('should allowing mutating headers', function (done) {
var token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36);
axios('/foo', {
transformRequest: function (data, headers) {
headers['X-Authorization'] = token;
}
});
getAjaxRequest().then(function (request) {
expect(request.requestHeaders['X-Authorization']).toEqual(token);
done();
});
});
});