Skip to content

Commit 763b281

Browse files
authored
test: test headers merging (#987)
1 parent f1354ee commit 763b281

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

test/integration/stamp.ux.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ test('Utils.getAmountForDuration', () => {
2121
})
2222

2323
test('bee.getStorageCost', async () => {
24-
await mocked(async (bee: Bee) => {
24+
await mocked(async bee => {
2525
const bzz = await bee.getStorageCost(Size.fromGigabytes(4), Duration.fromDays(1))
2626
expect(bzz.toSignificantDigits(3)).toBe('0.192')
2727
})
2828
})
2929

3030
test('bee.getDurationExtensionCost', async () => {
31-
await mocked(async (bee: Bee) => {
31+
await mocked(async bee => {
3232
const cost = await bee.getDurationExtensionCost(
3333
'f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
3434
Duration.fromDays(31),
@@ -38,7 +38,7 @@ test('bee.getDurationExtensionCost', async () => {
3838
})
3939

4040
test('bee.getSizeExtensionCost', async () => {
41-
await mocked(async (bee: Bee) => {
41+
await mocked(async bee => {
4242
const cost = await bee.getSizeExtensionCost(
4343
'f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
4444
Size.fromGigabytes(18),
@@ -76,7 +76,7 @@ test('bee.buyStorage with extensions', async () => {
7676
'New depth has to be greater than the original depth',
7777
)
7878
})
79-
expect(calls).toEqual([
79+
expect(calls.map(x => `${x.method} ${x.url}`)).toEqual([
8080
// create stamp
8181
'GET /chainstate',
8282
'GET /chainstate',

test/mocks.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,25 @@ responses.set(
7777
}),
7878
)
7979

80-
export async function mocked(runnable: (bee: Bee) => Promise<void>): Promise<string[]> {
81-
const calls: string[] = []
82-
return new Promise((resolve, reject) => {
80+
responses.set(
81+
'POST /bzz?name=filename.txt',
82+
JSON.stringify({
83+
reference: 'f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
84+
}),
85+
)
86+
87+
interface MockedCall {
88+
method: string
89+
url: string
90+
headers: Record<string, string | string[] | undefined>
91+
}
92+
93+
export async function mocked(runnable: (bee: Bee) => Promise<void>): Promise<MockedCall[]> {
94+
const calls: MockedCall[] = []
95+
return new Promise(resolve => {
8396
const server = createServer((req, res) => {
8497
const identifier = (req.method || 'GET') + ' ' + (req.url || '/')
85-
calls.push(identifier)
98+
calls.push({ method: req.method || 'GET', url: req.url || '/', headers: req.headers })
8699
const response = responses.get(identifier)
87100
if (!response) {
88101
res.end('Not found - ' + identifier)

test/regression/bee-js-986.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { mocked } from '../mocks'
2+
import { batch } from '../utils'
3+
4+
test('bee-js/986 - Headers not merging properly', async () => {
5+
const calls = await mocked(async bee => {
6+
await bee.uploadFile(
7+
batch(),
8+
'Does not matter',
9+
'filename.txt',
10+
{ act: true, tag: 1337, encrypt: true },
11+
{ headers: {} },
12+
)
13+
await bee.uploadFile(
14+
batch(),
15+
'Does not matter',
16+
'filename.txt',
17+
{ act: true, tag: 1338, encrypt: true },
18+
{
19+
headers: {
20+
arbitrary: 'value',
21+
},
22+
},
23+
)
24+
})
25+
expect(calls).toHaveLength(2)
26+
expect(calls[0]).toEqual({
27+
headers: {
28+
accept: 'application/json, text/plain, */*',
29+
connection: 'keep-alive',
30+
'content-length': '15',
31+
'content-type': 'application/x-www-form-urlencoded',
32+
host: 'localhost:11633',
33+
'swarm-act': 'true',
34+
'swarm-encrypt': 'true',
35+
'swarm-postage-batch-id': 'f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
36+
'swarm-tag': '1337',
37+
'user-agent': 'axios/0.28.1',
38+
},
39+
method: 'POST',
40+
url: '/bzz?name=filename.txt',
41+
})
42+
expect(calls[1]).toEqual({
43+
headers: {
44+
accept: 'application/json, text/plain, */*',
45+
arbitrary: 'value',
46+
connection: 'keep-alive',
47+
'content-length': '15',
48+
'content-type': 'application/x-www-form-urlencoded',
49+
host: 'localhost:11633',
50+
'swarm-act': 'true',
51+
'swarm-encrypt': 'true',
52+
'swarm-postage-batch-id': 'f8b2ad296d64824a8fe51a33ff15fe8668df13a20ad3d4eea4bb97ca600029aa',
53+
'swarm-tag': '1338',
54+
'user-agent': 'axios/0.28.1',
55+
},
56+
method: 'POST',
57+
url: '/bzz?name=filename.txt',
58+
})
59+
})

0 commit comments

Comments
 (0)