Skip to content

Commit

Permalink
fix: exclude headers not included in script
Browse files Browse the repository at this point in the history
  • Loading branch information
e-fisher committed Nov 25, 2024
1 parent ed84be5 commit d3ed6c3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { generateOptions } from './options'
import { getContentTypeWithCharsetHeader } from '@/utils/headers'
import { REQUIRED_IMPORTS } from '@/constants/imports'
import { generateImportStatement } from './imports'
import { cleanupRecording } from './codegen.utils'
import { cleanupRecording, shouldIncludeHeaderInScript } from './codegen.utils'
import { groupProxyData } from '@/utils/groups'

interface GenerateScriptParams {
Expand Down Expand Up @@ -186,9 +186,8 @@ function generateSleep(timing: ThinkTime['timing']): string {
}

export function generateRequestParams(request: ProxyData['request']): string {
const headersToExclude = ['Cookie', 'User-Agent', 'Host', 'Content-Length']
const headers = request.headers
.filter(([name]) => !headersToExclude.includes(name))
.filter(([name]) => shouldIncludeHeaderInScript(name))
.map(([name, value]) => `'${name}': \`${value}\``)
.join(',')

Expand Down
7 changes: 7 additions & 0 deletions src/codegen/codegen.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { flow } from 'lodash-es'
import { ProxyData } from '@/types'
import { getLocationHeader, getUpgradeHeader } from '@/utils/headers'
import { canonicalHeaderKey } from '@/rules/utils'

const HEADERS_TO_EXCLUDE = ['Cookie', 'User-Agent', 'Host', 'Content-Length']

// TODO: find a well-maintained library for this
export function stringify(value: unknown): string {
Expand Down Expand Up @@ -120,3 +123,7 @@ export const removeWebsocketRequests = (recording: ProxyData[]) => {
export function cleanupRecording(recording: ProxyData[]) {
return flow(removeWebsocketRequests, mergeRedirects)(recording)
}

export function shouldIncludeHeaderInScript(key: string) {
return !HEADERS_TO_EXCLUDE.includes(canonicalHeaderKey(key))
}
10 changes: 9 additions & 1 deletion src/views/Generator/RuleEditor/HeaderSelect.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Filter } from '@/types/rules'
import { matchFilter } from '@/rules/utils'
import { ProxyData } from '@/types'
import { useMemo } from 'react'
import { shouldIncludeHeaderInScript } from '@/codegen/codegen.utils'

export function useHeaderOptions(
recording: ProxyData[],
Expand All @@ -21,9 +22,16 @@ export function useHeaderOptions(
new Set(headers.map((header) => header[0]))
)

return uniqueHeaderNames.sort().map((headerName) => ({
const options = uniqueHeaderNames.sort().map((headerName) => ({
value: headerName,
label: headerName,
}))

// Don't show headers that are excluded from the codegen
if (extractFrom === 'request') {
return options.filter(({ value }) => shouldIncludeHeaderInScript(value))
}

return options
}, [recording, extractFrom, filter])
}

0 comments on commit d3ed6c3

Please sign in to comment.