Skip to content

Commit

Permalink
Merge pull request #731 from kuzzleio/fix/http-protocol-empty-responses
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauNeko authored Nov 7, 2023
2 parents e4bdf50 + 2d99ca6 commit 7391b80
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/actions/es-lint/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ runs:
using: "composite"
steps:
- name: Install deps
run: npm i -g npm && npm install
run: npm ci
shell: bash
- name: Run lint
run: npm run test:lint
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/functional-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ runs:
shell: bash
- name: Build Kuzzle
run: |
npm install
npm ci
npm run build
shell: bash
- name: Run functional tests
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/snippet-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ runs:
steps:
- name: Build the Stack
run: |
npm install
npm ci
npm run build
shell: bash
- run: npm run doc-testing
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/unit-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ runs:
steps:
- name: Run build
run: |
npm install
npm ci
npm run build
shell: bash
- name: Run tests
Expand Down
14 changes: 12 additions & 2 deletions src/protocols/Http.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

import staticHttpRoutes from "./routes.json";
import { KuzzleAbstractProtocol } from "./abstract/Base";
import { HttpRoutes, JSONObject } from "../types";
import { RequestPayload } from "../types/RequestPayload";
import { KuzzleAbstractProtocol } from "./abstract/Base";
import staticHttpRoutes from "./routes.json";

/**
* Http protocol used to connect to a Kuzzle server.
Expand Down Expand Up @@ -405,6 +405,11 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
);
}

const contentType = response.headers["content-type"];
if (!contentType || !contentType.includes("application/json")) {
return response.body;
}

return JSON.parse(response.body);
});
}
Expand Down Expand Up @@ -435,6 +440,11 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {

xhr.onload = () => {
try {
const contentType = xhr.getResponseHeader("Content-Type");
if (!contentType || !contentType.includes("application/json")) {
resolve(xhr.responseText);
return;
}
const json = JSON.parse(xhr.responseText);
resolve(json);
} catch (err) {
Expand Down
10 changes: 7 additions & 3 deletions test/protocol/Http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,12 @@ describe("HTTP networking module", () => {
let httpRequestStub;

beforeEach(() => {
httpRequestStub = sinon
.stub()
.resolves({ body: JSON.stringify(mockResponseBody) });
httpRequestStub = sinon.stub().resolves({
body: JSON.stringify(mockResponseBody),
headers: {
"content-type": "application/json",
},
});

const { default: MockHttp } = proxyquire("../../src/protocols/Http", {
"min-req-promise": { request: httpRequestStub },
Expand Down Expand Up @@ -793,6 +796,7 @@ describe("HTTP networking module", () => {
open: sinon.stub(),
send: sinon.stub(),
setRequestHeader: sinon.stub(),
getResponseHeader: sinon.stub().returns("application/json"),
onreadystatechange: sinon.stub(),
timeout: 0,
};
Expand Down

0 comments on commit 7391b80

Please sign in to comment.