|
| 1 | +var createJSON = "{\"firstName\":\"Test\",\"lastName\":\"User\",\"dateOfBirth\": \"10/07/1971\"}"; |
| 2 | +var updateJSON = "{\"firstName\":\"Test2\",\"lastName\":\"User2\",\"dateOfBirth\": \"10/07/1971\"}"; |
| 3 | +var newPersonId = ''; |
| 4 | + |
| 5 | +var http = require('http'); |
| 6 | +var TestFixture = require('nodeunit').testCase; |
| 7 | + |
| 8 | +module.exports['HTTP Method'] = TestFixture({ |
| 9 | + setUp: function (callBack) { |
| 10 | + |
| 11 | + this.localhost = http.createClient(3000, 'localhost'); |
| 12 | + |
| 13 | + this.requestHelper = function(request, fn){ |
| 14 | + request.end(); |
| 15 | + |
| 16 | + request.on('response', function (response) { |
| 17 | + var responseBody = ""; |
| 18 | + response.setEncoding('utf8'); |
| 19 | + |
| 20 | + response.addListener("data", function(chunk) { |
| 21 | + responseBody += chunk; |
| 22 | + }); |
| 23 | + |
| 24 | + response.on('end', function() { |
| 25 | + response.body = responseBody; |
| 26 | + fn(response); |
| 27 | + }); |
| 28 | + }); |
| 29 | + }; |
| 30 | + |
| 31 | + callBack(); |
| 32 | + }, |
| 33 | + |
| 34 | + tearDown: function (callBack) { |
| 35 | + // clean up |
| 36 | + callBack(); |
| 37 | + }, |
| 38 | + |
| 39 | + 'POST Should create a new Person' : function(test){ |
| 40 | + var request = this.localhost.request('POST', '/People.json', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'}); |
| 41 | + request.write(createJSON); |
| 42 | + |
| 43 | + this.requestHelper(request, function(response){ |
| 44 | + var actualPerson = JSON.parse(response.body); |
| 45 | + var expectedPerson = JSON.parse(createJSON); |
| 46 | + |
| 47 | + newPersonId = actualPerson._id; |
| 48 | + //console.log(actualPerson); |
| 49 | + |
| 50 | + test.ok(newPersonId != null); |
| 51 | + test.equals(expectedPerson.firstName, actualPerson.firstName); |
| 52 | + test.equals(expectedPerson.lastName, actualPerson.lastName); |
| 53 | +// test.equals(new Date(expectedPerson.dateOfBirth), new Date(actualPerson.dateOfBirth)); |
| 54 | + |
| 55 | + test.equals(response.statusCode, 201); |
| 56 | + |
| 57 | + test.done(); |
| 58 | + }); |
| 59 | + }, |
| 60 | + |
| 61 | + 'GET Should return a single Person when calling /People/{ID}.json' : function(test){ |
| 62 | + var request = this.localhost.request('GET', '/People/' + newPersonId + '.json', {'Host': 'localhost', 'Accept': 'application/json'}); |
| 63 | + |
| 64 | + this.requestHelper(request, function(response){ |
| 65 | + var actualPerson = JSON.parse(response.body); |
| 66 | + var expectedPerson = JSON.parse(createJSON); |
| 67 | + |
| 68 | + test.equals(expectedPerson.firstName, actualPerson.firstName); |
| 69 | + test.equals(expectedPerson.lastName, actualPerson.lastName); |
| 70 | +// test.equals(expectedPerson.dateOfBirth, actualPerson.dateOfBirth); |
| 71 | + |
| 72 | + test.equals(response.statusCode, 200); |
| 73 | + test.done(); |
| 74 | + }); |
| 75 | + }, |
| 76 | + |
| 77 | + 'GET Should get a Bad Request when calling /People/{ID}.xml' : function(test){ |
| 78 | + var request = this.localhost.request('GET', '/People/' + newPersonId + '.xml', {'Host': 'localhost', 'Accept': 'application/json'}); |
| 79 | + |
| 80 | + this.requestHelper(request, function(response){ |
| 81 | + test.equals(response.statusCode, 400); |
| 82 | + test.done(); |
| 83 | + }); |
| 84 | + }, |
| 85 | + |
| 86 | + 'PUT Should update an existing Person' : function(test){ |
| 87 | + var request = this.localhost.request('PUT', '/People/' + newPersonId + '.json', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'}); |
| 88 | + request.write(updateJSON); |
| 89 | + |
| 90 | + this.requestHelper(request, function(response){ |
| 91 | + var actualPerson = JSON.parse(response.body); |
| 92 | + var expectedPerson = JSON.parse(updateJSON); |
| 93 | + |
| 94 | + test.equals(expectedPerson.firstName, actualPerson.firstName); |
| 95 | + test.equals(expectedPerson.lastName, actualPerson.lastName); |
| 96 | +// test.equals(expectedPerson.dateOfBirth, actualPerson.dateOfBirth); |
| 97 | + |
| 98 | + test.equals(response.statusCode, 200); |
| 99 | + |
| 100 | + test.done(); |
| 101 | + }); |
| 102 | + }, |
| 103 | + |
| 104 | + 'PUT Should return 404 when trying to Update Person That Doesn\'t Exist' : function(test){ |
| 105 | + var request = this.localhost.request('PUT', '/People/XXXXX.json', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'}); |
| 106 | + request.write(updateJSON); |
| 107 | + |
| 108 | + this.requestHelper(request, function(response){ |
| 109 | + test.ok(response.body.length > 0); |
| 110 | + test.equals(response.statusCode, 404); |
| 111 | + test.done(); |
| 112 | + }); |
| 113 | + }, |
| 114 | + |
| 115 | + 'GET Should return all people when calling /People.json' : function(test){ |
| 116 | + var request = this.localhost.request('GET', '/People.json', {'Host': 'localhost', 'Accept': 'application/json'}); |
| 117 | + |
| 118 | + this.requestHelper(request, function(response){ |
| 119 | + test.equals(response.statusCode, 200); |
| 120 | + test.ok(response.body.length > 0); |
| 121 | + test.done(); |
| 122 | + }); |
| 123 | + }, |
| 124 | + |
| 125 | + 'GET Should return a 404 when calling /People/{ID} with an ID that doesn\'t exist' : function(test){ |
| 126 | + var request = this.localhost.request('GET', '/People/XXXXX.json', {'Host': 'localhost', 'Accept': 'application/json'}); |
| 127 | + |
| 128 | + this.requestHelper(request, function(response){ |
| 129 | + test.ok(response.body.length > 0); |
| 130 | + test.equals(response.statusCode, 404); |
| 131 | + test.done(); |
| 132 | + }); |
| 133 | + }, |
| 134 | + |
| 135 | + 'DELETE Should delete person when calling /People/{ID}' : function(test){ |
| 136 | + var request = this.localhost.request('DELETE', '/People/' + newPersonId, {'Host': 'localhost', 'Accept': 'application/json'}); |
| 137 | + |
| 138 | + this.requestHelper(request, function(response){ |
| 139 | + test.equals(response.statusCode, 200); |
| 140 | + test.done(); |
| 141 | + }); |
| 142 | + }, |
| 143 | + |
| 144 | + 'DELETE Should return a 404 when calling /People/{ID} with an ID that doesn\'t exist' : function(test){ |
| 145 | + var request = this.localhost.request('DELETE', '/People/XXXXX', {'Host': 'localhost', 'Accept': 'application/json'}); |
| 146 | + |
| 147 | + this.requestHelper(request, function(response){ |
| 148 | + test.equals(response.statusCode, 404); |
| 149 | + test.done(); |
| 150 | + }); |
| 151 | + } |
| 152 | +}); |
0 commit comments