本文整理汇总了TypeScript中fetch-mock.sandbox函数的典型用法代码示例。如果您正苦于以下问题:TypeScript sandbox函数的具体用法?TypeScript sandbox怎么用?TypeScript sandbox使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sandbox函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: test
test('handle request', (t) => {
let fetch = FetchMock.sandbox()
let { app, server } = makeHttpServer(config)
server.authTimestampCache = new MockAuthTimestampCache()
let sk = testPairs[1]
let fileContents = sk.toWIF()
let blob = Buffer.from(fileContents)
let address = ecPairToAddress(sk)
let path = `/store/${address}/helloWorld`
let listPath = `/list-files/${address}`
let prefix = ''
let authorizationHeader = ''
request(app).get('/hub_info/')
.expect(200)
.then((response) => {
prefix = JSON.parse(response.text).read_url_prefix
const challenge = JSON.parse(response.text).challenge_text
const authPart = auth.V1Authentication.makeAuthPart(sk, challenge)
return `bearer ${authPart}`
})
.then((authorization) => {
authorizationHeader = authorization
return request(app).post(path)
.set('Content-Type', 'application/octet-stream')
.set('Authorization', authorization)
.send(blob)
.expect(202)
})
.then((response) => {
if (mockTest) {
addMockFetches(fetch, prefix, dataMap)
}
let url = JSON.parse(response.text).publicURL
t.ok(url, 'Must return URL')
console.log(url)
fetch(url)
.then(resp => resp.text())
.then(text => t.equal(text, fileContents, 'Contents returned must be correct'))
})
.catch((err: any) => t.false(true, `Unexpected err: ${err}`))
.then(() => request(app).post(listPath)
.set('Content-Type', 'application/json')
.set('Authorization', authorizationHeader)
.expect(202)
)
.then((filesResponse) => {
const files = JSON.parse(filesResponse.text)
t.equal(files.entries.length, 1, 'Should return one file')
t.equal(files.entries[0], 'helloWorld', 'Should be helloworld')
t.ok(files.hasOwnProperty('page'), 'Response is missing a page')
})
.then(() => {
fetch.restore()
t.end()
})
})
开发者ID:blockstack,项目名称:blockstack-registrar,代码行数:59,代码来源:testHttp.ts
示例2:
.mock("http://test.com", 200)
.catch(503);
fetchMock
.mock("http://test.com", 200)
.spy();
const myMatcher: fetchMock.MockMatcherFunction = (
url: string,
opts: fetchMock.MockRequest
) => true;
fetchMock.flush().then(resolved => resolved.forEach(console.log));
fetchMock.flush().catch(r => r);
fetchMock.get("http://test.com", {
body: 'abc',
includeContentLength: false
});
fetchMock.get("http://test.com", {
body: 'abc',
redirectUrl: "http://example.org"
});
const sandbox = fetchMock.sandbox();
sandbox.get("http://test.com", {
body: 'abc',
redirectUrl: "http://example.org"
});
开发者ID:VincentDamour,项目名称:DefinitelyTyped,代码行数:30,代码来源:fetch-mock-tests.ts
注:本文中的fetch-mock.sandbox函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论