Skip to content

Commit b5763cc

Browse files
committed
wip
1 parent 0ed210b commit b5763cc

File tree

3 files changed

+23
-52
lines changed

3 files changed

+23
-52
lines changed

packages/signals/signals-integration-tests/src/tests/signals-vanilla/network-signals-fetch.test.ts

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test.describe('network signals - fetch', () => {
1111
await indexPage.loadAndWait(page, basicEdgeFn)
1212
})
1313

14-
test('should not emit non-json requests without the body', async () => {
14+
test('should not emit non-json requests', async () => {
1515
await indexPage.network.mockTestRoute('http://localhost/upload', {
1616
body: JSON.stringify({ foo: 'test' }),
1717
})
@@ -27,12 +27,10 @@ test.describe('network signals - fetch', () => {
2727
(el) => el.properties!.data.action === 'request'
2828
)
2929

30-
expect(requests).toHaveLength(1)
31-
expect(requests[0].properties!.data.url).toBe('http://localhost/upload')
32-
expect(requests[0].properties!.data.data).toBeNull()
30+
expect(requests).toHaveLength(0)
3331
})
3432

35-
test('should handle text/plain', async () => {
33+
test('should try to parse the body of text/plain requests', async () => {
3634
await indexPage.network.mockTestRoute('http://localhost/test', {
3735
body: JSON.stringify({ foo: 'test' }),
3836
contentType: 'application/json',
@@ -61,7 +59,7 @@ test.describe('network signals - fetch', () => {
6159
})
6260
})
6361

64-
test('should send the body as string if it cant be parsed to json', async () => {
62+
test('should send the raw string if the request body cannot be parsed as json', async () => {
6563
await indexPage.network.mockTestRoute('http://localhost/test', {
6664
body: JSON.stringify({ foo: 'test' }),
6765
contentType: 'application/json',
@@ -170,39 +168,6 @@ test.describe('network signals - fetch', () => {
170168
})
171169
})
172170

173-
test('should emit response but not request if request content-type is not json but response is', async () => {
174-
await indexPage.network.mockTestRoute('http://localhost/test', {
175-
body: JSON.stringify({ foo: 'test' }),
176-
contentType: 'application/json',
177-
})
178-
179-
await indexPage.network.makeFetchCall('http://localhost/test', {
180-
method: 'POST',
181-
body: 'hello world',
182-
contentType: 'text/plain',
183-
})
184-
185-
await indexPage.waitForSignalsApiFlush()
186-
187-
const networkEvents = indexPage.signalsAPI.getEvents('network')
188-
189-
const responses = networkEvents.filter(
190-
(el) => el.properties!.data.action === 'response'
191-
)
192-
expect(responses).toHaveLength(1)
193-
expect(responses[0].properties!.data).toMatchObject({
194-
action: 'response',
195-
url: 'http://localhost/test',
196-
data: { foo: 'test' },
197-
})
198-
199-
// Ensure no request was captured
200-
const requests = networkEvents.filter(
201-
(el) => el.properties!.data.action === 'request'
202-
)
203-
expect(requests).toHaveLength(0)
204-
})
205-
206171
test.describe('errors', () => {
207172
test('will handle a json error response', async () => {
208173
await indexPage.network.mockTestRoute('http://localhost/test', {
@@ -286,11 +251,7 @@ test.describe('network signals - fetch', () => {
286251
status: 400,
287252
})
288253

289-
await indexPage.network.makeFetchCall('http://localhost/test', {
290-
method: 'POST',
291-
body: 'hello world',
292-
contentType: 'text/plain',
293-
})
254+
await indexPage.network.makeFileUploadRequest('http://localhost/test')
294255

295256
await indexPage.waitForSignalsApiFlush()
296257

packages/signals/signals/src/core/signal-generators/network-gen/helpers.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,22 @@ export const containsContentType = (
5454
)
5555
}
5656

57-
export const containsJSONParseableContentType = (
58-
headers: HeadersInit | undefined
57+
export const containsJSONContentType = (
58+
Headers: HeadersInit | undefined
5959
): boolean => {
60-
return containsContentType(headers, [
60+
return containsContentType(Headers, [
6161
'application/json',
6262
'application/ld+json',
63-
'text/plain',
6463
'text/json',
6564
])
6665
}
6766

68-
export function isPlainObject(obj: unknown): obj is Record<string, unknown> {
67+
export const containsJSONParseableContentType = (
68+
headers: HeadersInit | undefined
69+
): boolean => {
6970
return (
70-
Object.prototype.toString.call(obj).slice(8, -1).toLowerCase() === 'object'
71+
containsJSONContentType(headers) ||
72+
containsContentType(headers, ['text/plain'])
7173
)
7274
}
7375

packages/signals/signals/src/core/signal-generators/network-gen/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212
NetworkRequestHandler,
1313
NetworkResponseHandler,
1414
} from './network-interceptor'
15-
import { containsJSONParseableContentType, tryJSONParse } from './helpers'
15+
import {
16+
containsJSONContentType,
17+
containsJSONParseableContentType,
18+
tryJSONParse,
19+
} from './helpers'
1620

1721
export type NetworkSettingsConfigSettings = Pick<
1822
SignalsSettingsConfig,
@@ -54,6 +58,10 @@ export class NetworkGenerator implements SignalGenerator {
5458
})
5559

5660
const handleRequest: NetworkRequestHandler = (rq) => {
61+
if (!containsJSONParseableContentType(rq.headers)) {
62+
return
63+
}
64+
5765
if (!rq.url || !this.filter.isAllowed(rq.url)) {
5866
return
5967
}
@@ -79,7 +87,7 @@ export class NetworkGenerator implements SignalGenerator {
7987
const isSuccessWithNonJSONResponse =
8088
rs.ok &&
8189
rs.responseType !== 'json' &&
82-
!containsJSONParseableContentType(rs.headers)
90+
!containsJSONContentType(rs.headers)
8391

8492
const isErrorButRequestNeverEmittted =
8593
!rs.ok && !this.emittedRequestIds.includes(rs.req.id)

0 commit comments

Comments
 (0)