headers 模块单元测试
大约 1 分钟约 360 字
headers 模块单元测试
之前我们测试了 headers
的基础方法模块,接下来我们会从业务角度测试 headers
的相关业务逻辑。
测试代码编写
test/headers.spec.ts
:
import axios from '../src/index'
import { getAjaxRequest } from './helper'
function testHeaderValue(headers: any, key: string, val?: string): void {
let found = false
for (let k in headers) {
if (k.toLowerCase() === key.toLowerCase()) {
found = true
expect(headers[k]).toBe(val)
break
}
}
if (!found) {
if (typeof val === 'undefined') {
expect(headers.hasOwnProperty(key)).toBeFalsy()
} else {
throw new Error(key + ' was not found in headers')
}
}
}
describe('headers', () => {
beforeEach(() => {
jasmine.Ajax.install()
})
afterEach(() => {
jasmine.Ajax.uninstall()
})
test('should use default common headers', () => {
const headers = axios.defaults.headers.common
axios('/foo')
return getAjaxRequest().then(request => {
for (let key in headers) {
if (headers.hasOwnProperty(key)) {
expect(request.requestHeaders[key]).toEqual(headers[key])
}
}
})
})
test('should add extra headers for post', () => {
axios.post('/foo', 'fizz=buzz')
return getAjaxRequest().then(request => {
testHeaderValue(
request.requestHeaders,
'Content-Type',
'application/x-www-form-urlencoded'
)
})
})
test('should use application/json when posting an object', () => {
axios.post('/foo/bar', {
firstName: 'foo',
lastName: 'bar'
})
return getAjaxRequest().then(request => {
testHeaderValue(
request.requestHeaders,
'Content-Type',
'application/json;charset=utf-8'
)
})
})
test('should remove content-type if data is empty', () => {
axios.post('/foo')
return getAjaxRequest().then(request => {
testHeaderValue(request.requestHeaders, 'Content-Type', undefined)
})
})
it('should preserve content-type if data is false', () => {
axios.post('/foo', false)
return getAjaxRequest().then(request => {
testHeaderValue(
request.requestHeaders,
'Content-Type',
'application/x-www-form-urlencoded'
)
})
})
test('should remove content-type if data is FormData', () => {
const data = new FormData()
data.append('foo', 'bar')
axios.post('/foo', data)
return getAjaxRequest().then(request => {
testHeaderValue(request.requestHeaders, 'Content-Type', undefined)
})
})
})
内部定义了 testHeaderValue
辅助函数,用于测试 headers
是否存在某个 header name
下的某个值。
至此我们完成了 ts-axios
库 headers
模块相关业务逻辑的测试,下一节课我们会对 Axios
的实例做测试。