From 012144b7b8ea31a00714cb555c8c900fdece9b55 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Mon, 14 May 2018 11:46:15 +0300 Subject: [PATCH] Add tests --- LICENSE | 1 - test/ExponentialTime.spec.ts | 2 +- test/SimpleWebRequest.spec.ts | 74 +++++++++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/LICENSE b/LICENSE index 4248002..605ec0a 100644 --- a/LICENSE +++ b/LICENSE @@ -21,4 +21,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/test/ExponentialTime.spec.ts b/test/ExponentialTime.spec.ts index 9d16884..df6ac12 100644 --- a/test/ExponentialTime.spec.ts +++ b/test/ExponentialTime.spec.ts @@ -100,4 +100,4 @@ describe('ExponentialTime', () => { expect(() => new ExponentialTime(INITIAL_TIME, MAX_TIME, DEFAULT_TIME_JITTER, -1)) .toThrowError('Jitter factor must be non-negative'); }); -}); \ No newline at end of file +}); diff --git a/test/SimpleWebRequest.spec.ts b/test/SimpleWebRequest.spec.ts index 6ab834f..8fa6005 100644 --- a/test/SimpleWebRequest.spec.ts +++ b/test/SimpleWebRequest.spec.ts @@ -6,8 +6,8 @@ describe('SimpleWebRequest', () => { beforeEach(() => jasmine.Ajax.install()); afterEach(() => jasmine.Ajax.uninstall()); - it('performs simple request', () => { - const requestOptions = { priority: 0, contentType: 'json' }; + it('performs GET request', () => { + const requestOptions = { contentType: 'json' }; const requestHeaders = { 'Accept': 'application/json' }; const statusCode = 200; const onSuccess = jasmine.createSpy('onSuccess'); @@ -28,5 +28,71 @@ describe('SimpleWebRequest', () => { expect(onSuccess).toHaveBeenCalledWith(response); }) - // @TODO ... -}); \ No newline at end of file + it('sends json POST request', () => { + const sendData = { + title: faker.name.title(), + text: faker.lorem.text(), + }; + const requestOptions = { sendData }; + const statusCode = 201; + const onSuccess = jasmine.createSpy('onSuccess'); + const method = 'POST'; + const body = { ...sendData, id: faker.random.uuid() }; + const url = faker.internet.url(); + const response = { ...DETAILED_RESPONSE, requestOptions, statusCode, method, body, url }; + + new SimpleWebRequest(method, url, requestOptions) + .start() + .then(onSuccess); + + const request = jasmine.Ajax.requests.mostRecent(); + request.respondWith({ responseText: JSON.stringify(body), status: statusCode }); + + expect(request.url).toEqual(url); + expect(request.method).toEqual(method); + expect(request.status).toEqual(statusCode); + expect(onSuccess).toHaveBeenCalledWith(response); + }) + + it('allows to set request headers', () => { + const headers = { + 'X-Requested-With': 'XMLHttpRequest', + 'Max-Forwards': '10' + }; + const method = 'POST'; + const url = faker.internet.url(); + + new SimpleWebRequest(url, method, {}, () => headers).start(); + + const request = jasmine.Ajax.requests.mostRecent(); + + expect(request.requestHeaders['X-Requested-With']).toEqual(headers['X-Requested-With']); + expect(request.requestHeaders['Max-Forwards']).toEqual(headers['Max-Forwards']); + }); + + it('forbids to set Accept header', () => { + const headers = { + 'Accept': 'application/xml', + }; + const method = 'GET'; + const url = faker.internet.url(); + + expect( + () => new SimpleWebRequest(url, method, {}, () => headers).start() + ).toThrowError(`Don't set Accept with options.headers -- use it with the options.acceptType property`) + }) + + it('forbids to set Content-Type header', () => { + const headers = { + 'Content-Type': 'application/xml', + }; + const method = 'GET'; + const url = faker.internet.url(); + + expect( + () => new SimpleWebRequest(url, method, {}, () => headers).start() + ).toThrowError(`Don't set Content-Type with options.headers -- use it with the options.contentType property`) + }) + + // @TODO Add more unit tests +});