fix: ensure Content-Type header priority in the CLI (#4242)
- Ensure the `Content-Type` header takes priority over the value set in the request body. - Introduces `HoppRESTRequest` schema `v6` with `text/xml` added under the supported content types.
This commit is contained in:
@@ -149,6 +149,16 @@ describe("hopp test [options] <file_path_or_id>", () => {
|
||||
|
||||
expect(error).toBeNull();
|
||||
});
|
||||
|
||||
test("The `Content-Type` header takes priority over the value set at the request body", async () => {
|
||||
const args = `test ${getTestJsonFilePath(
|
||||
"content-type-header-scenarios.json",
|
||||
"collection"
|
||||
)}`;
|
||||
const { error } = await runCLI(args);
|
||||
|
||||
expect(error).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test `hopp test <file_path_or_id> --env <file_path_or_id>` command:", () => {
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
{
|
||||
"v": 2,
|
||||
"name": "content-type-header-scenarios",
|
||||
"folders": [],
|
||||
"requests": [
|
||||
{
|
||||
"v": "6",
|
||||
"auth": {
|
||||
"authType": "inherit",
|
||||
"authActive": true
|
||||
},
|
||||
"body": {
|
||||
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>\n <user>\n <id>12345</id>\n <name>John Doe</name>\n <email>john.doe@example.com</email>\n </user>\n <order>\n <id>98765</id>\n <product>Sample Product</product>\n <quantity>2</quantity>\n </order>\n</request>\n",
|
||||
"contentType": "text/xml"
|
||||
},
|
||||
"name": "content-type-header-assignment",
|
||||
"method": "POST",
|
||||
"params": [],
|
||||
"headers": [],
|
||||
"endpoint": "https://echo.hoppscotch.io",
|
||||
"testScript": "pw.test(\"The `Content-Type` header is assigned the content type value set at the request body level\", ()=> {\n pw.expect(pw.response.body.headers[\"content-type\"]).toBe(\"text/xml\");\n});",
|
||||
"preRequestScript": "",
|
||||
"requestVariables": []
|
||||
},
|
||||
{
|
||||
"v": "6",
|
||||
"auth": {
|
||||
"authType": "inherit",
|
||||
"authActive": true
|
||||
},
|
||||
"body": {
|
||||
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>\n <user>\n <id>12345</id>\n <name>John Doe</name>\n <email>john.doe@example.com</email>\n </user>\n <order>\n <id>98765</id>\n <product>Sample Product</product>\n <quantity>2</quantity>\n </order>\n</request>\n",
|
||||
"contentType": "application/json"
|
||||
},
|
||||
"name": "content-type-header-override",
|
||||
"method": "POST",
|
||||
"params": [],
|
||||
"headers": [
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "application/xml",
|
||||
"active": true
|
||||
}
|
||||
],
|
||||
"endpoint": "https://echo.hoppscotch.io",
|
||||
"testScript": "pw.test(\"The `Content-Type` header overrides the content type value set at the request body level\", ()=> {\n pw.expect(pw.response.body.headers[\"content-type\"]).toBe(\"application/xml\");\n});",
|
||||
"preRequestScript": "",
|
||||
"requestVariables": []
|
||||
},
|
||||
{
|
||||
"v": "6",
|
||||
"auth": {
|
||||
"authType": "inherit",
|
||||
"authActive": true
|
||||
},
|
||||
"body": {
|
||||
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>\n <user>\n <id>12345</id>\n <name>John Doe</name>\n <email>john.doe@example.com</email>\n </user>\n <order>\n <id>98765</id>\n <product>Sample Product</product>\n <quantity>2</quantity>\n </order>\n</request>\n",
|
||||
"contentType": "application/json"
|
||||
},
|
||||
"name": "multiple-content-type-headers",
|
||||
"method": "POST",
|
||||
"params": [],
|
||||
"headers": [
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "text/xml",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "application/json",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "application/xml",
|
||||
"active": true
|
||||
}
|
||||
],
|
||||
"endpoint": "https://echo.hoppscotch.io",
|
||||
"testScript": "pw.test(\"The last occurrence will be considered among multiple `Content-Type` headers\", ()=> {\n pw.expect(pw.response.body.headers[\"content-type\"]).toBe(\"application/xml\");\n});",
|
||||
"preRequestScript": "",
|
||||
"requestVariables": []
|
||||
},
|
||||
{
|
||||
"v": "6",
|
||||
"auth": {
|
||||
"authType": "inherit",
|
||||
"authActive": true
|
||||
},
|
||||
"body": {
|
||||
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>\n <user>\n <id>12345</id>\n <name>John Doe</name>\n <email>john.doe@example.com</email>\n </user>\n <order>\n <id>98765</id>\n <product>Sample Product</product>\n <quantity>2</quantity>\n </order>\n</request>\n",
|
||||
"contentType": null
|
||||
},
|
||||
"name": "multiple-content-type-headers-different-casing",
|
||||
"method": "POST",
|
||||
"params": [],
|
||||
"headers": [
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "text/xml",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "content-Type",
|
||||
"value": "application/json",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "Content-type",
|
||||
"value": "text/plain",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "CONTENT-TYPE",
|
||||
"value": "application/xml",
|
||||
"active": true
|
||||
}
|
||||
],
|
||||
"endpoint": "https://echo.hoppscotch.io",
|
||||
"testScript": "pw.test(\"The last occurrence will be considered among multiple `Content-Type` headers following different casing\", ()=> {\n pw.expect(pw.response.body.headers[\"content-type\"]).toBe(\"application/xml\");\n});",
|
||||
"preRequestScript": "",
|
||||
"requestVariables": []
|
||||
},
|
||||
{
|
||||
"v": "6",
|
||||
"auth": {
|
||||
"authType": "inherit",
|
||||
"authActive": true
|
||||
},
|
||||
"body": {
|
||||
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>\n <user>\n <id>12345</id>\n <name>John Doe</name>\n <email>john.doe@example.com</email>\n </user>\n <order>\n <id>98765</id>\n <product>Sample Product</product>\n <quantity>2</quantity>\n </order>\n</request>\n",
|
||||
"contentType": null
|
||||
},
|
||||
"name": "multiple-content-type-headers-different-casing-without-value-set-at-body",
|
||||
"method": "POST",
|
||||
"params": [],
|
||||
"headers": [
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "text/xml",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "content-Type",
|
||||
"value": "application/json",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "Content-type",
|
||||
"value": "text/plain",
|
||||
"active": true
|
||||
},
|
||||
{
|
||||
"key": "CONTENT-TYPE",
|
||||
"value": "application/xml",
|
||||
"active": true
|
||||
}
|
||||
],
|
||||
"endpoint": "https://echo.hoppscotch.io",
|
||||
"testScript": "pw.test(\"The content type is inferred from the `Content-Type` header if not set at the request body\", ()=> {\n pw.expect(pw.response.body.headers[\"content-type\"]).toBe(\"application/xml\");\n});",
|
||||
"preRequestScript": "",
|
||||
"requestVariables": []
|
||||
}
|
||||
],
|
||||
"auth": {
|
||||
"authType": "inherit",
|
||||
"authActive": true
|
||||
},
|
||||
"headers": []
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
Environment,
|
||||
EnvironmentSchemaVersion,
|
||||
HoppCollection,
|
||||
RESTReqSchemaVersion,
|
||||
} from "@hoppscotch/data";
|
||||
|
||||
import {
|
||||
@@ -78,8 +79,7 @@ export const WORKSPACE_DEEPLY_NESTED_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Workspa
|
||||
collectionID: "clx1ldkzs005t10f8rp5u60q7",
|
||||
teamID: "clws3hg58000011o8h07glsb1",
|
||||
title: "RequestA",
|
||||
request:
|
||||
'{"v":"5","id":"clpttpdq00003qp16kut6doqv","auth":{"authType":"inherit","authActive":true},"body":{"body":null,"contentType":null},"name":"RequestA","method":"GET","params":[],"headers":[],"endpoint":"https://echo.hoppscotch.io","testScript":"pw.test(\\"Correctly inherits auth and headers from the root collection\\", ()=> {\\n pw.expect(pw.response.body.headers[\\"x-test-header\\"]).toBe(\\"Set at root collection\\");\\n pw.expect(pw.response.body.headers[\\"authorization\\"]).toBe(\\"Bearer BearerToken\\");\\n});","preRequestScript":"","requestVariables":[]}',
|
||||
request: `{"v":"${RESTReqSchemaVersion}","id":"clpttpdq00003qp16kut6doqv","auth":{"authType":"inherit","authActive":true},"body":{"body":null,"contentType":null},"name":"RequestA","method":"GET","params":[],"headers":[],"endpoint":"https://echo.hoppscotch.io","testScript":"pw.test(\\"Correctly inherits auth and headers from the root collection\\", ()=> {\\n pw.expect(pw.response.body.headers[\\"x-test-header\\"]).toBe(\\"Set at root collection\\");\\n pw.expect(pw.response.body.headers[\\"authorization\\"]).toBe(\\"Bearer BearerToken\\");\\n});","preRequestScript":"","requestVariables":[]}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -214,7 +214,7 @@ export const TRANSFORMED_DEEPLY_NESTED_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: HoppC
|
||||
],
|
||||
requests: [
|
||||
{
|
||||
v: "5",
|
||||
v: RESTReqSchemaVersion,
|
||||
id: "clpttpdq00003qp16kut6doqv",
|
||||
auth: {
|
||||
authType: "inherit",
|
||||
|
||||
@@ -162,12 +162,18 @@ export function getEffectiveRESTRequest(
|
||||
}
|
||||
const effectiveFinalBody = _effectiveFinalBody.right;
|
||||
|
||||
if (request.body.contentType)
|
||||
if (
|
||||
request.body.contentType &&
|
||||
!effectiveFinalHeaders.some(
|
||||
({ key }) => key.toLowerCase() === "content-type"
|
||||
)
|
||||
) {
|
||||
effectiveFinalHeaders.push({
|
||||
active: true,
|
||||
key: "content-type",
|
||||
key: "Content-Type",
|
||||
value: request.body.contentType,
|
||||
});
|
||||
}
|
||||
|
||||
// Parsing final-endpoint with applied ENVs.
|
||||
const _effectiveFinalURL = parseTemplateStringE(
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { Environment, HoppCollection, HoppRESTRequest } from "@hoppscotch/data";
|
||||
import axios, { Method } from "axios";
|
||||
import {
|
||||
Environment,
|
||||
HoppCollection,
|
||||
HoppRESTRequest,
|
||||
RESTReqSchemaVersion,
|
||||
} from "@hoppscotch/data";
|
||||
import axios, { AxiosResponse, Method } from "axios";
|
||||
import * as A from "fp-ts/Array";
|
||||
import * as E from "fp-ts/Either";
|
||||
import * as T from "fp-ts/Task";
|
||||
@@ -55,8 +60,8 @@ const processVariables = (variable: Environment["variables"][number]) => {
|
||||
const processEnvs = (envs: Partial<HoppEnvs>) => {
|
||||
// This can take the shape `{ global: undefined, selected: undefined }` when no environment is supplied
|
||||
const processedEnvs = {
|
||||
global: envs.global?.map(processVariables),
|
||||
selected: envs.selected?.map(processVariables),
|
||||
global: envs.global?.map(processVariables) ?? [],
|
||||
selected: envs.selected?.map(processVariables) ?? [],
|
||||
};
|
||||
|
||||
return processedEnvs;
|
||||
@@ -92,9 +97,12 @@ export const createRequest = (req: EffectiveHoppRESTRequest): RequestConfig => {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (req.body.contentType) {
|
||||
config.headers["Content-Type"] = req.body.contentType;
|
||||
switch (req.body.contentType) {
|
||||
|
||||
const resolvedContentType =
|
||||
config.headers["Content-Type"] ?? req.body.contentType;
|
||||
|
||||
if (resolvedContentType) {
|
||||
switch (resolvedContentType) {
|
||||
case "multipart/form-data": {
|
||||
// TODO: Parse Multipart Form Data
|
||||
// !NOTE: Temporary `config.supported` check
|
||||
@@ -166,7 +174,7 @@ export const requestRunner =
|
||||
};
|
||||
|
||||
if (axios.isAxiosError(e)) {
|
||||
runnerResponse.endpoint = e.config.url ?? "";
|
||||
runnerResponse.endpoint = e.config?.url ?? "";
|
||||
|
||||
if (e.response) {
|
||||
const { data, status, statusText, headers } = e.response;
|
||||
@@ -358,7 +366,7 @@ export const preProcessRequest = (
|
||||
const { headers: parentHeaders, auth: parentAuth } = collection;
|
||||
|
||||
if (!tempRequest.v) {
|
||||
tempRequest.v = "1";
|
||||
tempRequest.v = RESTReqSchemaVersion;
|
||||
}
|
||||
if (!tempRequest.name) {
|
||||
tempRequest.name = "Untitled Request";
|
||||
|
||||
Reference in New Issue
Block a user