2016-05-13 23:05:16 +03:00
|
|
|
import * as hpm from '../src/httpPostMessage';
|
|
|
|
|
|
|
|
describe("HttpPostMessage", function () {
|
2016-05-26 20:49:40 +03:00
|
|
|
let windowPostMessageProxy: hpm.IPostMessage;
|
2016-05-13 23:05:16 +03:00
|
|
|
let httpPostMessage: hpm.HttpPostMessage;
|
|
|
|
const postMessage = (message: any): Promise<any> => {
|
|
|
|
return Promise.resolve(message);
|
|
|
|
};
|
|
|
|
const postMessageSpy = jasmine.createSpy("postMessageSpy").and.callFake(postMessage);
|
|
|
|
|
|
|
|
beforeAll(function () {
|
|
|
|
windowPostMessageProxy = {
|
|
|
|
postMessage: postMessageSpy
|
|
|
|
};
|
|
|
|
|
2016-06-17 22:55:00 +03:00
|
|
|
httpPostMessage = new hpm.HttpPostMessage(window, windowPostMessageProxy);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// emtpy
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
postMessageSpy.calls.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(function () {
|
|
|
|
// empty
|
|
|
|
});
|
|
|
|
|
|
|
|
it("get() calls windowPostMessageProxy.postMessage with GET request", function () {
|
|
|
|
// Arrange
|
|
|
|
const expectedRequest = {
|
|
|
|
method: "GET",
|
|
|
|
url: 'report/pages',
|
|
|
|
headers: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
httpPostMessage.get(expectedRequest.url);
|
|
|
|
|
|
|
|
// Assert
|
2016-06-17 22:55:00 +03:00
|
|
|
expect(postMessageSpy).toHaveBeenCalledWith(window, expectedRequest);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("post() calls windowPostMessageProxy.postMessage with POST request", function () {
|
|
|
|
// Arrange
|
|
|
|
const expectedRequest = {
|
|
|
|
method: "POST",
|
|
|
|
url: 'report/filters',
|
|
|
|
headers: {},
|
|
|
|
body: {
|
|
|
|
testBody: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
httpPostMessage.post(expectedRequest.url, expectedRequest.body);
|
|
|
|
|
|
|
|
// Assert
|
2016-06-17 22:55:00 +03:00
|
|
|
expect(postMessageSpy).toHaveBeenCalledWith(window, expectedRequest);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("put() calls windowPostMessageProxy.postMessage with PUT request", function () {
|
|
|
|
// Arrange
|
|
|
|
const expectedRequest = {
|
|
|
|
method: "PUT",
|
|
|
|
url: 'report/pages/activepage',
|
|
|
|
headers: {},
|
|
|
|
body: {
|
|
|
|
testBody: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
httpPostMessage.put(expectedRequest.url, expectedRequest.body);
|
|
|
|
|
|
|
|
// Assert
|
2016-06-17 22:55:00 +03:00
|
|
|
expect(postMessageSpy).toHaveBeenCalledWith(window, expectedRequest);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("patch() calls windowPostMessageProxy.postMessage with PATCH request", function () {
|
|
|
|
// Arrange
|
|
|
|
const expectedRequest = {
|
|
|
|
method: "PATCH",
|
|
|
|
url: 'report/pages/activepage',
|
|
|
|
headers: {},
|
|
|
|
body: {
|
|
|
|
testBody: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
httpPostMessage.patch(expectedRequest.url, expectedRequest.body);
|
|
|
|
|
|
|
|
// Assert
|
2016-06-17 22:55:00 +03:00
|
|
|
expect(postMessageSpy).toHaveBeenCalledWith(window, expectedRequest);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
2016-06-08 23:15:54 +03:00
|
|
|
|
|
|
|
it("delete() calls windowPostMessageProxy.postMessage with DELETE request", function () {
|
|
|
|
// Arrange
|
|
|
|
const expectedRequest = {
|
|
|
|
method: "DELETE",
|
|
|
|
url: 'report/pages/activepage',
|
|
|
|
headers: {},
|
|
|
|
body: {
|
|
|
|
testBody: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
httpPostMessage.delete(expectedRequest.url, expectedRequest.body);
|
|
|
|
|
|
|
|
// Assert
|
2016-06-17 22:55:00 +03:00
|
|
|
expect(postMessageSpy).toHaveBeenCalledWith(window, expectedRequest);
|
2016-06-08 23:15:54 +03:00
|
|
|
});
|
2016-05-13 23:05:16 +03:00
|
|
|
|
|
|
|
describe("custom headers", function () {
|
|
|
|
let defaultHeaders: any;
|
|
|
|
let httpPostMessageProxyWithDefaultHeaders: hpm.HttpPostMessage;
|
|
|
|
|
|
|
|
beforeAll(function () {
|
|
|
|
defaultHeaders = {
|
|
|
|
'custom-header-1': 'customValue',
|
|
|
|
'request-id': 'abc123'
|
|
|
|
};
|
2016-06-17 22:55:00 +03:00
|
|
|
httpPostMessageProxyWithDefaultHeaders = new hpm.HttpPostMessage(window, windowPostMessageProxy, defaultHeaders);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("default headers can be set, which will be included with each request", function () {
|
|
|
|
// Arrange
|
|
|
|
const expectedRequest = {
|
|
|
|
method: "GET",
|
|
|
|
url: 'report/pages',
|
|
|
|
headers: defaultHeaders
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
httpPostMessageProxyWithDefaultHeaders.get(expectedRequest.url);
|
|
|
|
|
|
|
|
// Assert
|
2016-06-17 22:55:00 +03:00
|
|
|
expect(postMessageSpy).toHaveBeenCalledWith(window, expectedRequest);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
|
2016-05-24 03:33:26 +03:00
|
|
|
it("all the methods can have optional parameter to pass other custom headers for the single request", function () {
|
2016-05-13 23:05:16 +03:00
|
|
|
// Arrange
|
|
|
|
const singleRequestHeaders = {
|
|
|
|
'new-custom-header': 'newCustomValue'
|
|
|
|
};
|
|
|
|
const headers = $.extend({}, defaultHeaders, singleRequestHeaders);
|
|
|
|
headers['new-custom-header'] = 'newCustomValue';
|
|
|
|
|
|
|
|
const expectedRequest = {
|
|
|
|
method: "GET",
|
|
|
|
url: 'report/pages',
|
|
|
|
headers
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
httpPostMessageProxyWithDefaultHeaders.get(expectedRequest.url, singleRequestHeaders);
|
|
|
|
|
|
|
|
// Assert
|
2016-06-17 22:55:00 +03:00
|
|
|
expect(postMessageSpy).toHaveBeenCalledWith(window, expectedRequest);
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("message manipulation", function () {
|
|
|
|
it("addTrackingProperties should add a custom id header to each message", function () {
|
|
|
|
// Arrange
|
|
|
|
const testMessage = {
|
|
|
|
method: "GET",
|
|
|
|
url: "report/pages",
|
|
|
|
headers: {}
|
|
|
|
};
|
|
|
|
const trackingProperties = {
|
|
|
|
id: (Math.random() + 1).toString(36).substring(7)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const resultMessage = hpm.HttpPostMessage.addTrackingProperties(testMessage, trackingProperties);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(resultMessage.headers.id).toEqual(trackingProperties.id);
|
|
|
|
});
|
2016-06-22 21:42:44 +03:00
|
|
|
|
|
|
|
it("addTrackingProperties will set header.id to undefined if trackingProperties or trackingProperties.id provided is falsy", function () {
|
|
|
|
// Arrange
|
|
|
|
const testMessage = {
|
|
|
|
method: "GET",
|
|
|
|
url: "report/pages",
|
|
|
|
headers: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const resultMessage = hpm.HttpPostMessage.addTrackingProperties(testMessage, { x: 'abc' });
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(resultMessage.headers.id).toBe(undefined);
|
|
|
|
});
|
2016-05-13 23:05:16 +03:00
|
|
|
|
|
|
|
it("getTrackingProperties should return tracking properties object by fetching id from headers of message", function () {
|
|
|
|
// Arrange
|
|
|
|
const testMessage = {
|
|
|
|
method: "GET",
|
|
|
|
url: "report/pages",
|
|
|
|
headers: {
|
|
|
|
id: (Math.random() + 1).toString(36).substring(7)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedTrackingProperties = {
|
|
|
|
id: testMessage.headers.id
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const actualTrackingProperties = hpm.HttpPostMessage.getTrackingProperties(testMessage);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(actualTrackingProperties).toEqual(expectedTrackingProperties);
|
|
|
|
});
|
2016-05-24 03:33:26 +03:00
|
|
|
|
|
|
|
it("isErrorMessage should return true for messages with statusCode outside of range [200-300)", function () {
|
|
|
|
expect(hpm.HttpPostMessage.isErrorMessage({ statusCode: 100 })).toEqual(true);
|
|
|
|
expect(hpm.HttpPostMessage.isErrorMessage({ statusCode: 200 })).toEqual(false);
|
|
|
|
expect(hpm.HttpPostMessage.isErrorMessage({ statusCode: 300 })).toEqual(true);
|
|
|
|
expect(hpm.HttpPostMessage.isErrorMessage({ statusCode: 400 })).toEqual(true);
|
|
|
|
expect(hpm.HttpPostMessage.isErrorMessage({ statusCode: 500 })).toEqual(true);
|
2016-06-17 22:55:00 +03:00
|
|
|
});
|
2016-05-13 23:05:16 +03:00
|
|
|
});
|
|
|
|
});
|