Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support request chaining for body #738

Merged
merged 5 commits into from
Jun 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions src/components/probe/probing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ export async function probing(
): Promise<ProbeRequestResponse> {
// Compile URL using handlebars to render URLs that uses previous responses data
const { method, url, headers, timeout, body, ping } = requestConfig
const newReq = { method, headers, timeout, ping }
const newReq = { method, headers, timeout, body, ping }
const renderURL = Handlebars.compile(url)
const renderedURL = renderURL({ responses })
let requestBody: any = body

// Compile headers using handlebars to render URLs that uses previous responses data.
// In some case such as value is not string, it will be returned as is without being compiled.
Expand All @@ -63,10 +62,7 @@ export async function probing(

// evaluate "Content-Type" header in case-insensitive manner
if (header.toLocaleLowerCase() === 'content-type') {
const { content, contentType } = transformContentByType(body, rawHeader)

// change request body with the transformed request body
requestBody = content
const { contentType } = transformContentByType(body, rawHeader)

if (rawHeader === 'multipart/form-data') {
// delete the previous content-type header and add a new header with boundary
Expand All @@ -86,6 +82,44 @@ export async function probing(
}
}

if (body) {
if (typeof body === 'string') {
const renderBody = Handlebars.compile(body)
const renderedBody = renderBody({ responses })

newReq.body = renderedBody as any
} else {
for (const bk of Object.keys(body)) {
const rawBody = (body as any)[bk]
const renderBody = Handlebars.compile(rawBody)
const renderedBody = renderBody({ responses })

newReq.body = { ...newReq.body, [bk]: renderedBody }
}
}

if (newReq.headers) {
const contentTypeKey = Object.keys(headers).find((hk) => {
return hk.toLocaleLowerCase() === 'content-type'
})

if (contentTypeKey) {
const { content, contentType } = transformContentByType(
newReq.body,
headers[contentTypeKey]
)

delete newReq.headers[contentTypeKey]

newReq.body = content
newReq.headers = {
...newReq.headers,
'content-type': contentType,
}
}
}
}

const axiosInstance = axios.create()
const requestStartedAt = Date.now()

Expand Down Expand Up @@ -121,7 +155,7 @@ export async function probing(
const resp = await axiosInstance.request({
...newReq,
url: renderedURL,
data: requestBody,
data: newReq.body,
})

const responseTime = Date.now() - requestStartedAt
Expand Down