Skip to content

Commit

Permalink
make HTTP request headers optional (#260)
Browse files Browse the repository at this point in the history
Extra HTTP request headers are usually not required. This change makes
them optional and makes it so that the HTTP driver only requires the
`url` field to perform a request. This also adds tests for using default
HTTP requests that are both successful and result in an error.
  • Loading branch information
kbrsh authored Oct 12, 2019
2 parents 3d7174e + 9838956 commit 6eb9010
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
8 changes: 5 additions & 3 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,12 @@

xhr.open("method" in request ? request.method : "GET", request.url); // Set request headers.

var requestHeaders = request.headers;
if ("headers" in request) {
var requestHeaders = request.headers;

for (var requestHeader in requestHeaders) {
xhr.setRequestHeader(requestHeader, requestHeaders[requestHeader]);
for (var requestHeader in requestHeaders) {
xhr.setRequestHeader(requestHeader, requestHeaders[requestHeader]);
}
} // Send the request with the given body.


Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions packages/moon/src/http/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export default {
);

// Set request headers.
const requestHeaders = request.headers;
if ("headers" in request) {
const requestHeaders = request.headers;

for (const requestHeader in requestHeaders) {
xhr.setRequestHeader(requestHeader, requestHeaders[requestHeader]);
for (const requestHeader in requestHeaders) {
xhr.setRequestHeader(requestHeader, requestHeaders[requestHeader]);
}
}

// Send the request with the given body.
Expand Down
62 changes: 62 additions & 0 deletions packages/moon/test/executor/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ date: Fri, 25 Jun 04 00:00:00 +0000`;
};

test("successful http request", () => {
let run = false;
error = false;

Moon.use({
Expand All @@ -36,6 +37,7 @@ test("successful http request", () => {
Moon.run(() => ({
http: [{
method: "GET",
responseType: "text",
url: "https://example.com",
headers: {
test: "moon request",
Expand All @@ -44,6 +46,7 @@ test("successful http request", () => {
},
body: "Moon Test Request",
onLoad: ({ http }) => {
run = true;
expect(setRequestHeader).toBeCalledWith("test", "moon request");
expect(setRequestHeader).toBeCalledWith("foo", "bar");
expect(setRequestHeader).toBeCalledWith("date", "Fri, 25 Jun 04 00:00:00 +0000");
Expand All @@ -58,9 +61,16 @@ test("successful http request", () => {
}
}]
}));

expect(run).toEqual(true);

setRequestHeader.mockClear();
open.mockClear();
send.mockClear();
});

test("failing http request", () => {
let run = false;
error = true;

Moon.use({
Expand All @@ -70,6 +80,7 @@ test("failing http request", () => {
Moon.run(() => ({
http: [{
method: "GET",
responseType: "text",
url: "https://example.com",
headers: {
test: "moon request",
Expand All @@ -78,6 +89,7 @@ test("failing http request", () => {
},
body: "Moon Test Request",
onError: ({ http }) => {
run = true;
expect(setRequestHeader).toBeCalledWith("test", "moon request");
expect(setRequestHeader).toBeCalledWith("foo", "bar");
expect(setRequestHeader).toBeCalledWith("date", "Fri, 25 Jun 04 00:00:00 +0000");
Expand All @@ -88,4 +100,54 @@ test("failing http request", () => {
}
}]
}));

expect(run).toEqual(true);

setRequestHeader.mockClear();
open.mockClear();
send.mockClear();
});

test("default http request", () => {
error = false;

Moon.use({
http: Moon.http.driver
});

Moon.run(() => ({
http: [{
url: "https://example.com"
}]
}));

expect(setRequestHeader).not.toBeCalled();
expect(open).toBeCalledWith("GET", "https://example.com");
expect(send).toBeCalledWith(null);

setRequestHeader.mockClear();
open.mockClear();
send.mockClear();
});

test("default http request with error", () => {
error = true;

Moon.use({
http: Moon.http.driver
});

Moon.run(() => ({
http: [{
url: "https://example.com"
}]
}));

expect(setRequestHeader).not.toBeCalled();
expect(open).toBeCalledWith("GET", "https://example.com");
expect(send).toBeCalledWith(null);

setRequestHeader.mockClear();
open.mockClear();
send.mockClear();
});

0 comments on commit 6eb9010

Please sign in to comment.