|
| 1 | +import { assert } from 'chai' |
| 2 | +import execute from '../src/execute' |
| 3 | +import { httpclient } from '../src/index' |
| 4 | +import sinon, { SinonFakeServer } from 'sinon' |
| 5 | + |
| 6 | +describe('execute', function() { |
| 7 | + let server: SinonFakeServer |
| 8 | + describe('execute with a real API external', function() { |
| 9 | + before(function() { |
| 10 | + server = sinon.fakeServer.create() |
| 11 | + }) |
| 12 | + after(function() { |
| 13 | + server.restore() |
| 14 | + }) |
| 15 | + |
| 16 | + it('should be able to create an entry', async function() { |
| 17 | + const aRequest = new httpclient.Request('http://dummy.restapiexample.com/api/v1/create') |
| 18 | + aRequest.method = 'POST' |
| 19 | + aRequest.body = { |
| 20 | + 'name': 'auDD', |
| 21 | + 'salary': '333000', |
| 22 | + 'age': '22' |
| 23 | + } |
| 24 | + server.respondWith( |
| 25 | + 'POST', |
| 26 | + 'http://dummy.restapiexample.com/api/v1/create', |
| 27 | + [ |
| 28 | + 200, |
| 29 | + { 'Content-Type': 'application/json' }, |
| 30 | + JSON.stringify({ id: 'anID' }) |
| 31 | + ] |
| 32 | + ) |
| 33 | + const postResponsePromises = execute(aRequest) |
| 34 | + server.respond() |
| 35 | + const postResponse = await postResponsePromises |
| 36 | + assert.equal((postResponse as any).status, 200) |
| 37 | + }) |
| 38 | + it('should be able to find the entry we posted before', async function() { |
| 39 | + |
| 40 | + server.respondWith( |
| 41 | + 'GET', |
| 42 | + 'http://dummy.restapiexample.com/api/v1/employees', |
| 43 | + [ |
| 44 | + 200, |
| 45 | + { 'Content-Type': 'application/json' }, |
| 46 | + JSON.stringify([ { |
| 47 | + 'name': 'auDD', |
| 48 | + 'salary': '333000', |
| 49 | + 'age': '22' |
| 50 | + }]) |
| 51 | + ] |
| 52 | + ) |
| 53 | + |
| 54 | + const aRequest = new httpclient.Request('http://dummy.restapiexample.com/api/v1/employees') |
| 55 | + const postResponsePromises = execute<any[]>(aRequest) |
| 56 | + server.respond() |
| 57 | + const getResponse = await postResponsePromises |
| 58 | + const employees = getResponse.body |
| 59 | + assert.deepEqual(employees, [ { |
| 60 | + 'name': 'auDD', |
| 61 | + 'salary': '333000', |
| 62 | + 'age': '22' |
| 63 | + }]) |
| 64 | + }) |
| 65 | + it('should be able to modify the entry we added', async function() { |
| 66 | + const expectedResponse = [ { |
| 67 | + 'name': 'auDD', |
| 68 | + 'salary': '333000', |
| 69 | + 'age': '22' |
| 70 | + }] |
| 71 | + server.respondWith( |
| 72 | + 'PUT', |
| 73 | + 'http://dummy.restapiexample.com/api/v1/update/anID', |
| 74 | + [ |
| 75 | + 200, |
| 76 | + { 'Content-Type': 'application/json' }, |
| 77 | + JSON.stringify(expectedResponse) |
| 78 | + ] |
| 79 | + ) |
| 80 | + const aRequest = new httpclient.Request('http://dummy.restapiexample.com/api/v1/update/anID') |
| 81 | + aRequest.method = 'PUT' |
| 82 | + aRequest.body = { |
| 83 | + 'name': 'pnl', |
| 84 | + 'salary': '1000000', |
| 85 | + 'age': '22' |
| 86 | + } |
| 87 | + const postResponsePromises = execute(aRequest) |
| 88 | + server.respond() |
| 89 | + const putResponse = await postResponsePromises |
| 90 | + assert.deepEqual(putResponse.body, expectedResponse) |
| 91 | + |
| 92 | + }) |
| 93 | + it('should be able to delete the entry we modified before', async function() { |
| 94 | + server.respondWith( |
| 95 | + 'DELETE', |
| 96 | + 'http://dummy.restapiexample.com/api/v1/delete/idOfCreatedObject', |
| 97 | + [ |
| 98 | + 200, |
| 99 | + { 'Content-Type': 'application/json' }, |
| 100 | + JSON.stringify({ expectedResponse: 'any' }) |
| 101 | + ] |
| 102 | + ) |
| 103 | + const aRequest = new httpclient.Request('http://dummy.restapiexample.com/api/v1/delete/idOfCreatedObject') |
| 104 | + aRequest.method = 'DELETE' |
| 105 | + const postResponsePromises = execute(aRequest) |
| 106 | + server.respond() |
| 107 | + const response = await postResponsePromises |
| 108 | + // |
| 109 | + |
| 110 | + assert.deepEqual(response.body, { expectedResponse: 'any' }) |
| 111 | + |
| 112 | + }) |
| 113 | + it('should throw an error if http request sent to bad url', async function() { |
| 114 | + try { |
| 115 | + server.respondWith( |
| 116 | + 'GET', |
| 117 | + 'http://dummy.restapiexample.com/i@#ccidently_mizspeld976theurl/', |
| 118 | + [ |
| 119 | + 404, |
| 120 | + { 'Content-Type': 'application/json' }, |
| 121 | + 'not found' |
| 122 | + ] |
| 123 | + ) |
| 124 | + const aRequest = new httpclient.Request('http://dummy.restapiexample.com/i@#ccidently_mizspeld976theurl/') |
| 125 | + const responsePromise = execute(aRequest) |
| 126 | + server.respond() |
| 127 | + const response = await responsePromise |
| 128 | + assert.isTrue(false, 'previous line should throw an error') |
| 129 | + } catch (error) { |
| 130 | + const response: httpclient.Response<Object> = error |
| 131 | + assert.equal(response.status, 404) |
| 132 | + } |
| 133 | + }) |
| 134 | + it('should also throw an error on server error', async function() { |
| 135 | + try { |
| 136 | + server.respondWith( |
| 137 | + 'PUT', |
| 138 | + 'http://dummy.restapiexample.com/api/v1/employees', |
| 139 | + [ |
| 140 | + 500, |
| 141 | + { 'Content-Type': 'application/json' }, |
| 142 | + 'server error' |
| 143 | + ] |
| 144 | + ) |
| 145 | + const aRequest = new httpclient.Request('http://dummy.restapiexample.com/api/v1/employees') |
| 146 | + aRequest.method = 'PUT' |
| 147 | + aRequest.body = { |
| 148 | + 'name': 'pnl', |
| 149 | + 'salary': '1000000', |
| 150 | + 'age': '22' |
| 151 | + } |
| 152 | + const responsePromise = execute(aRequest) |
| 153 | + server.respond() |
| 154 | + const response = await responsePromise |
| 155 | + assert.isTrue(false, 'previous line should throw an error') |
| 156 | + } catch (error) { |
| 157 | + const response: httpclient.Response<Object> = error |
| 158 | + assert.equal(response.status,500) |
| 159 | + } |
| 160 | + }) |
| 161 | + }) |
| 162 | +}) |
0 commit comments