Skip to content

Commit 7144952

Browse files
authored
fix: wrong content type for batch-analysis (#429)
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent 2ea1d77 commit 7144952

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/analysis.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { runLicenseCheck } from "./license/index.js";
66
import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js";
77
import { addProxyAgent, getCustom, getTokenHeaders , TRUSTIFY_DA_OPERATION_TYPE_HEADER, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER } from "./tools.js";
88

9+
/** Media type for CycloneDX JSON batch payloads (batch-analysis API). */
10+
export const CYCLONEDX_JSON_MEDIA_TYPE = 'application/vnd.cyclonedx+json'
11+
912
export default { requestComponent, requestStack, requestStackBatch, requestImages, validateToken }
1013

1114
/**
@@ -155,7 +158,7 @@ async function requestStackBatch(sbomByPurl, url, html = false, opts = {}) {
155158
method: 'POST',
156159
headers: {
157160
'Accept': html ? 'text/html' : 'application/json',
158-
'Content-Type': 'application/json',
161+
'Content-Type': CYCLONEDX_JSON_MEDIA_TYPE,
159162
...getTokenHeaders(opts)
160163
},
161164
body: JSON.stringify(sbomByPurl)
@@ -208,7 +211,7 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
208211
method: 'POST',
209212
headers: {
210213
'Accept': html ? 'text/html' : 'application/json',
211-
'Content-Type': 'application/vnd.cyclonedx+json',
214+
'Content-Type': CYCLONEDX_JSON_MEDIA_TYPE,
212215
...getTokenHeaders(opts)
213216
},
214217
body: JSON.stringify(imageSboms),

test/stack_analysis_batch.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import esmock from 'esmock'
66
import { http, HttpResponse } from 'msw'
77
import { setupServer } from 'msw/node'
88

9+
import { CYCLONEDX_JSON_MEDIA_TYPE } from '../src/analysis.js'
10+
911
const BACKEND_URL = 'http://localhost:9999'
1012
const BACKEND_OPTS = { TRUSTIFY_DA_BACKEND_URL: BACKEND_URL }
1113

@@ -73,13 +75,22 @@ function makeSbom(name, version) {
7375
}
7476
}
7577

78+
function mediaTypeWithoutParameters(contentType) {
79+
if (!contentType) {
80+
return ''
81+
}
82+
return contentType.split(';')[0].trim()
83+
}
84+
7685
suite('stackAnalysisBatch', () => {
7786
let server
7887
let capturedBody
88+
let capturedContentType
7989

8090
suiteSetup(() => {
8191
server = setupServer(
8292
http.post(`${BACKEND_URL}/api/v5/batch-analysis`, async ({ request }) => {
93+
capturedContentType = request.headers.get('content-type')
8394
capturedBody = await request.json()
8495
const report = {}
8596
for (const purl of Object.keys(capturedBody)) {
@@ -97,6 +108,7 @@ suite('stackAnalysisBatch', () => {
97108

98109
setup(() => {
99110
capturedBody = null
111+
capturedContentType = null
100112
})
101113

102114
test('discovers JS workspace packages, generates SBOMs, and sends batch request', async () => {
@@ -127,6 +139,7 @@ suite('stackAnalysisBatch', () => {
127139

128140
expect(result).to.be.an('object')
129141
expect(capturedBody).to.be.an('object')
142+
expect(mediaTypeWithoutParameters(capturedContentType)).to.equal(CYCLONEDX_JSON_MEDIA_TYPE)
130143
const purls = Object.keys(capturedBody)
131144
expect(purls).to.include('pkg:npm/app-a@1.0.0')
132145
expect(purls).to.include('pkg:npm/app-b@2.0.0')
@@ -164,6 +177,7 @@ suite('stackAnalysisBatch', () => {
164177
expect(result.metadata.ecosystem).to.equal('javascript')
165178
expect(result.metadata.successful).to.equal(2)
166179
expect(result.metadata.failed).to.equal(0)
180+
expect(mediaTypeWithoutParameters(capturedContentType)).to.equal(CYCLONEDX_JSON_MEDIA_TYPE)
167181
} finally {
168182
cleanup()
169183
}
@@ -196,6 +210,7 @@ suite('stackAnalysisBatch', () => {
196210
expect(result.metadata.failed).to.be.at.least(1)
197211
expect(result.metadata.errors.some(e => e.phase === 'validation')).to.be.true
198212
expect(capturedBody).to.be.an('object')
213+
expect(mediaTypeWithoutParameters(capturedContentType)).to.equal(CYCLONEDX_JSON_MEDIA_TYPE)
199214
expect(Object.keys(capturedBody)).to.include('pkg:npm/good@1.0.0')
200215
} finally {
201216
cleanup()
@@ -281,6 +296,7 @@ suite('stackAnalysisBatch', () => {
281296

282297
expect(result).to.be.an('object')
283298
expect(capturedBody).to.be.an('object')
299+
expect(mediaTypeWithoutParameters(capturedContentType)).to.equal(CYCLONEDX_JSON_MEDIA_TYPE)
284300
expect(Object.keys(capturedBody)).to.include('pkg:npm/web@1.0.0')
285301
} finally {
286302
cleanup()

0 commit comments

Comments
 (0)