2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-27 14:47:43 +03:00
Files
axios/test/specs/headers.spec.js
T
David Ko 920510b3a6 Bug/allow header to contain http verb keys #1252 (#1258)
* Failing test for #1252

* Only delete header keys that match an HTTP verb if the value is a non-string

Co-authored-by: David Ko <david.ko@pvtmethod.com>
Co-authored-by: Jay <jasonsaayman@gmail.com>
2020-05-22 21:26:10 +02:00

103 lines
2.5 KiB
JavaScript

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 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;charset=utf-8');
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();
});
});
describe('when a header key matches an HTTP verb', function() {
it('preserves the header value', function(done) {
axios.post('/foo', null, { 'headers': { 'delete': 'test' } });
getAjaxRequest().then(function (request) {
var headerValue = request.requestHeaders['delete'];
expect(headerValue).toEqual('test');
done();
});
});
});
});