-
-
Notifications
You must be signed in to change notification settings - Fork 11k
/
transform.spec.js
200 lines (166 loc) · 4.73 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import AxiosError from "../../lib/core/AxiosError";
describe('transform', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
it('should transform JSON to string', function (done) {
const 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) {
let 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) {
let 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');
expect(thrown.code).toEqual(AxiosError.ERR_BAD_RESPONSE);
done();
}, 100);
});
}
);
it('should send data as JSON if request content-type is application/json', function (done) {
let 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) {
let response;
axios.get('/foo', {
responseType: 'text',
transitional: {
forcedJSONParsing: false
}
}).then(function (_response) {
response = _response;
}, function (err) {
done(err);
});
const 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) {
const 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) {
const 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) {
const 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();
});
});
it('should normalize \'content-type\' header when using a custom transformRequest', function (done) {
const data = {
foo: 'bar'
};
axios.post('/foo', data, {
headers: { 'content-type': 'application/x-www-form-urlencoded' },
transformRequest: [
function () {
return 'aa=44'
}
]
});
getAjaxRequest().then(function (request) {
expect(request.requestHeaders['Content-Type']).toEqual('application/x-www-form-urlencoded');
done();
});
});
});