Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 31 additions & 5 deletions src/codegen/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('Code generation', () => {
},
{
type: 'correlation',
id: '1',
id: '2',
enabled: true,
extractor: {
filter: { path: '' },
Expand All @@ -296,6 +296,20 @@ describe('Code generation', () => {
extractionMode: 'single',
},
},
{
type: 'correlation',
id: '3',
enabled: true,
extractor: {
filter: { path: '' },
selector: {
type: 'json',
from: 'body',
path: 'is_admin',
},
extractionMode: 'single',
},
},
]

it('should replace correlated values', async () => {
Expand All @@ -321,30 +335,37 @@ describe('Code generation', () => {
correlation_vars["correlation_0"] = match[1];
}

correlation_vars['correlation_1'] = resp.json().is_admin

params = {
headers: {}, cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/login?project_id=\${correlation_vars['correlation_0']}\`
resp = http.request('POST', url, null, params)

correlation_vars['correlation_1'] = resp.json().user_id
correlation_vars['correlation_2'] = resp.json().user_id
})

group('two', function () {
params = {
headers: {}, cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/users/\${correlation_vars['correlation_1']}\`
url = http.url\`http://test.k6.io/api/v1/users/\${correlation_vars['correlation_2']}\`
resp = http.request('GET', url, null, params)

params = {
headers: {}, cookies: {}
}

url = http.url\`http://test.k6.io/api/v1/users\`
resp = http.request('POST', url, \`${JSON.stringify({ user_id: "${correlation_vars['correlation_1']}" })}\`, params)
resp = http.request(
'POST',
url,
\`{"user_id":"\${correlation_vars['correlation_2']}","is_admin":\${correlation_vars['correlation_1']}}\`,
params
)
})

sleep(1)
Expand Down Expand Up @@ -393,7 +414,12 @@ describe('Code generation', () => {
}

url = http.url\`http://test.k6.io/api/v1/users\`
resp = http.request('POST', url, \`${JSON.stringify({ user_id: '333' })}\`, params)
resp = http.request(
'POST',
url,
\`${JSON.stringify({ user_id: '333', is_admin: false })}\`,
params
)
})

sleep(1)
Expand Down
6 changes: 3 additions & 3 deletions src/rules/correlation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function applyRule({
// this is the modified schema that we return to the accumulator
const snippetSchemaReturnValue = cloneDeep(requestSnippetSchema)

if (state.extractedValue) {
if (state.extractedValue !== undefined && state.extractedValue !== '') {
// Skip replacement if replacer filter doesn't match
if (
rule.replacer &&
Expand Down Expand Up @@ -125,7 +125,7 @@ function applyRule({
idGenerator
)

if (extractedValue && correlationExtractionSnippet) {
if (extractedValue !== undefined && correlationExtractionSnippet) {
// Skip extraction and bump count if value is already extracted and we are in single extraction mode
if (state.extractedValue && rule.extractor.extractionMode === 'single') {
setState({
Expand Down Expand Up @@ -558,7 +558,7 @@ const extractCorrelationJsonBody = (
const extractedValue = getJsonObjectFromPath(response.content, selector.path)

if (
!extractedValue ||
extractedValue === undefined ||
(Array.isArray(extractedValue) && extractedValue.length === 0)
) {
return noCorrelationResult
Expand Down
23 changes: 22 additions & 1 deletion src/rules/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export const replaceJsonBody = (
// TODO: https://github.com/grafana/k6-studio/issues/277
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const valueToReplace = getJsonObjectFromPath(request.content, selector.path)
if (!valueToReplace) return
if (valueToReplace === undefined) return

const content = setJsonObjectFromPath(request.content, selector.path, value)
return { ...request, content }
Expand Down Expand Up @@ -528,27 +528,48 @@ if (import.meta.vitest) {
from: 'body',
path: '[0].hello',
}

expect(
replaceJsonBody(
selectorMatch,
generateRequest('{"hello":"world"}'),
'${correl_0}'
)?.content
).toBe('{"hello":"${correl_0}"}')

expect(
replaceJsonBody(
selectorNotMatch,
generateRequest('{"hello":"world"}'),
'${correl_0}'
)
).toBeUndefined()

expect(
replaceJsonBody(
selectorMatchArray,
generateRequest('[{"hello":"world"}]'),
'${correl_0}'
)?.content
).toBe('[{"hello":"${correl_0}"}]')

// Empty string replacement
expect(
replaceJsonBody(
selectorMatchArray,
generateRequest('[{"hello":""}]'),
'${correl_0}'
)?.content
).toBe('[{"hello":"${correl_0}"}]')

// Boolean replacement
expect(
replaceJsonBody(
selectorMatchArray,
generateRequest('[{"hello":false}]'),
'${correl_0}'
)?.content
).toBe('[{"hello":"${correl_0}"}]')
})

it('replaces header name matches', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/test/fixtures/correlationRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const correlationRecording: ProxyData[] = [
['project', 'project_id=555'],
],
cookies: [],
content: JSON.stringify({ user_id: '444' }),
content: JSON.stringify({ user_id: '444', is_admin: false }),
contentLength: 0,
timestampStart: 0,
},
Expand Down Expand Up @@ -95,7 +95,7 @@ export const correlationRecording: ProxyData[] = [
query: [],
scheme: 'http',
host: 'localhost:3000',
content: JSON.stringify({ user_id: '333' }),
content: JSON.stringify({ user_id: '333', is_admin: false }),
path: '/api/v1/users',
timestampStart: 0,
timestampEnd: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/views/Generator/RuleEditor/CorrelationEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function ExtractedValue({

const extractedValue = selectedRuleInstance?.state?.extractedValue

if (!extractedValue) {
if (extractedValue === undefined) {
return (
<Text size="2" color="gray">
The rule does not match any requests
Expand Down