|
| 1 | +var Request = require('../src/base-request'); |
| 2 | + |
| 3 | +describe('Create Requests', () => { |
| 4 | + test('Should create host, port, and scheme', () => { |
| 5 | + var request = Request.builder() |
| 6 | + .withHost('such.api.wow') |
| 7 | + .withPort(1337) |
| 8 | + .withScheme('http') |
| 9 | + .build(); |
| 10 | + |
| 11 | + expect(request.getHost()).toBe('such.api.wow'); |
| 12 | + expect(request.getPort()).toBe(1337); |
| 13 | + expect(request.getScheme()).toBe('http'); |
| 14 | + }); |
| 15 | + |
| 16 | + test('Should add query parameters', () => { |
| 17 | + var request = Request.builder() |
| 18 | + .withHost('such.api.wow') |
| 19 | + .withPort(1337) |
| 20 | + .withScheme('http') |
| 21 | + .withQueryParameters({ |
| 22 | + oneParameter: 1, |
| 23 | + anotherParameter: true, |
| 24 | + thirdParameter: 'hello' |
| 25 | + }) |
| 26 | + .build(); |
| 27 | + |
| 28 | + expect(request.getQueryParameters().oneParameter).toBe(1); |
| 29 | + expect(request.getQueryParameters().anotherParameter).toBe(true); |
| 30 | + expect(request.getQueryParameters().thirdParameter).toBe('hello'); |
| 31 | + }); |
| 32 | + |
| 33 | + test('Should add query parameters (multiple calls)', () => { |
| 34 | + var request = Request.builder() |
| 35 | + .withHost('such.api.wow') |
| 36 | + .withPort(1337) |
| 37 | + .withScheme('http') |
| 38 | + .withQueryParameters({ |
| 39 | + oneParameter: 1, |
| 40 | + anotherParameter: true |
| 41 | + }) |
| 42 | + .withQueryParameters({ |
| 43 | + thirdParameter: 'hello' |
| 44 | + }) |
| 45 | + .build(); |
| 46 | + |
| 47 | + expect(request.getQueryParameters().oneParameter).toBe(1); |
| 48 | + expect(request.getQueryParameters().anotherParameter).toBe(true); |
| 49 | + expect(request.getQueryParameters().thirdParameter).toBe('hello'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('Should add query parameters (combine calls)', () => { |
| 53 | + var request = Request.builder() |
| 54 | + .withHost('such.api.wow') |
| 55 | + .withPort(1337) |
| 56 | + .withScheme('http') |
| 57 | + .withQueryParameters( |
| 58 | + { |
| 59 | + oneParameter: 1, |
| 60 | + anotherParameter: true |
| 61 | + }, |
| 62 | + { |
| 63 | + thirdParameter: 'hello' |
| 64 | + } |
| 65 | + ) |
| 66 | + .build(); |
| 67 | + |
| 68 | + expect(request.getQueryParameters().oneParameter).toBe(1); |
| 69 | + expect(request.getQueryParameters().anotherParameter).toBe(true); |
| 70 | + expect(request.getQueryParameters().thirdParameter).toBe('hello'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Should add body parameters', () => { |
| 74 | + var request = Request.builder() |
| 75 | + .withHost('such.api.wow') |
| 76 | + .withPort(1337) |
| 77 | + .withScheme('http') |
| 78 | + .withBodyParameters({ |
| 79 | + one: 1, |
| 80 | + two: true, |
| 81 | + three: 'world' |
| 82 | + }) |
| 83 | + .build(); |
| 84 | + |
| 85 | + expect(request.getBodyParameters().one).toBe(1); |
| 86 | + expect(request.getBodyParameters().two).toBe(true); |
| 87 | + expect(request.getBodyParameters().three).toBe('world'); |
| 88 | + }); |
| 89 | + |
| 90 | + test('Should add array to body parameters', () => { |
| 91 | + var request = Request.builder() |
| 92 | + .withHost('such.api.wow') |
| 93 | + .withPort(1337) |
| 94 | + .withScheme('http') |
| 95 | + .withBodyParameters(['3VNWq8rTnQG6fM1eldSpZ0']) |
| 96 | + .build(); |
| 97 | + |
| 98 | + expect(request.getBodyParameters()).toEqual(['3VNWq8rTnQG6fM1eldSpZ0']); |
| 99 | + }); |
| 100 | + |
| 101 | + test('Should add header parameters', () => { |
| 102 | + var request = Request.builder() |
| 103 | + .withHost('such.api.wow') |
| 104 | + .withPort(1337) |
| 105 | + .withScheme('http') |
| 106 | + .withHeaders({ |
| 107 | + Authorization: 'Basic WOOP', |
| 108 | + 'Content-Type': 'application/lol' |
| 109 | + }) |
| 110 | + .build(); |
| 111 | + |
| 112 | + expect(request.getHeaders().Authorization).toBe('Basic WOOP'); |
| 113 | + expect(request.getHeaders()['Content-Type']).toBe('application/lol'); |
| 114 | + }); |
| 115 | + |
| 116 | + test('Should add path', () => { |
| 117 | + var request = Request.builder() |
| 118 | + .withHost('such.api.wow') |
| 119 | + .withPort(1337) |
| 120 | + .withPath('/v1/users/meriosweg') |
| 121 | + .build(); |
| 122 | + |
| 123 | + expect(request.getPath()).toBe('/v1/users/meriosweg'); |
| 124 | + }); |
| 125 | + |
| 126 | + test('Should build URI', () => { |
| 127 | + var request = Request.builder() |
| 128 | + .withHost('such.api.wow') |
| 129 | + .withScheme('https') |
| 130 | + .withPort(1337) |
| 131 | + .withPath('/v1/users/meriosweg') |
| 132 | + .build(); |
| 133 | + |
| 134 | + expect(request.getURI()).toBe( |
| 135 | + 'https://such.api.wow:1337/v1/users/meriosweg' |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + test('Should construct empty query paramaters string', () => { |
| 140 | + var request = Request.builder() |
| 141 | + .withQueryParameters({}) |
| 142 | + .build(); |
| 143 | + |
| 144 | + expect(request.getQueryParameterString()).toBeFalsy(); |
| 145 | + }); |
| 146 | + |
| 147 | + test('Should construct query paramaters string for one parameter', () => { |
| 148 | + var request = Request.builder() |
| 149 | + .withQueryParameters({ |
| 150 | + one: 1 |
| 151 | + }) |
| 152 | + .build(); |
| 153 | + |
| 154 | + expect(request.getQueryParameterString()).toBe('?one=1'); |
| 155 | + }); |
| 156 | + |
| 157 | + test('Should construct query paramaters string for multiple parameters', () => { |
| 158 | + var request = Request.builder() |
| 159 | + .withQueryParameters({ |
| 160 | + one: 1, |
| 161 | + two: true, |
| 162 | + three: 'world' |
| 163 | + }) |
| 164 | + .build(); |
| 165 | + |
| 166 | + expect(request.getQueryParameterString()).toBe( |
| 167 | + '?one=1&two=true&three=world' |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + test('Should construct query paramaters string and exclude undefined values', () => { |
| 172 | + var request = Request.builder() |
| 173 | + .withQueryParameters({ |
| 174 | + one: 1, |
| 175 | + two: undefined, |
| 176 | + three: 'world' |
| 177 | + }) |
| 178 | + .build(); |
| 179 | + |
| 180 | + expect(request.getQueryParameterString()).toBe('?one=1&three=world'); |
| 181 | + }); |
| 182 | +}); |
0 commit comments