|
| 1 | +import * as httpm from '../_out'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as am from '../_out/auth'; |
| 4 | +import * as fs from 'fs'; |
| 5 | + |
| 6 | +let sampleFilePath: string = path.join(__dirname, 'testoutput.txt'); |
| 7 | + |
| 8 | +describe('basics', () => { |
| 9 | + let _http: httpm.HttpClient; |
| 10 | + let _httpbin: httpm.HttpClient; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + _http = new httpm.HttpClient('http-client-tests'); |
| 14 | + }) |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + |
| 18 | + }) |
| 19 | + |
| 20 | + it('constructs', () => { |
| 21 | + let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests'); |
| 22 | + expect(http).toBeDefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + // responses from httpbin return something like: |
| 26 | + // { |
| 27 | + // "args": {}, |
| 28 | + // "headers": { |
| 29 | + // "Connection": "close", |
| 30 | + // "Host": "httpbin.org", |
| 31 | + // "User-Agent": "typed-test-client-tests" |
| 32 | + // }, |
| 33 | + // "origin": "173.95.152.44", |
| 34 | + // "url": "https://httpbin.org/get" |
| 35 | + // } |
| 36 | + |
| 37 | + it('does basic http get request', async() => { |
| 38 | + let res: httpm.HttpClientResponse = await _http.get('http://httpbin.org/get'); |
| 39 | + expect(res.message.statusCode).toBe(200); |
| 40 | + let body: string = await res.readBody(); |
| 41 | + let obj: any = JSON.parse(body); |
| 42 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 43 | + expect(obj.headers["User-Agent"]).toBeTruthy(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does basic http get request with no user agent', async() => { |
| 47 | + let http: httpm.HttpClient = new httpm.HttpClient(); |
| 48 | + let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get'); |
| 49 | + expect(res.message.statusCode).toBe(200); |
| 50 | + let body: string = await res.readBody(); |
| 51 | + let obj: any = JSON.parse(body); |
| 52 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 53 | + expect(obj.headers["User-Agent"]).toBeFalsy(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does basic https get request', async() => { |
| 57 | + let res: httpm.HttpClientResponse = await _http.get('https://httpbin.org/get'); |
| 58 | + expect(res.message.statusCode).toBe(200); |
| 59 | + let body: string = await res.readBody(); |
| 60 | + let obj: any = JSON.parse(body); |
| 61 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does basic http get request with default headers', async() => { |
| 65 | + let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [], { |
| 66 | + headers: { |
| 67 | + 'Accept': 'application/json', |
| 68 | + 'Content-Type': 'application/json' |
| 69 | + } |
| 70 | + }); |
| 71 | + let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get'); |
| 72 | + expect(res.message.statusCode).toBe(200); |
| 73 | + let body: string = await res.readBody(); |
| 74 | + let obj:any = JSON.parse(body); |
| 75 | + expect(obj.headers.Accept).toBe('application/json'); |
| 76 | + expect(obj.headers['Content-Type']).toBe('application/json'); |
| 77 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does basic http get request with merged headers', async() => { |
| 81 | + let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [], { |
| 82 | + headers: { |
| 83 | + 'Accept': 'application/json', |
| 84 | + 'Content-Type': 'application/json' |
| 85 | + } |
| 86 | + }); |
| 87 | + let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get', { |
| 88 | + 'content-type': 'application/x-www-form-urlencoded' |
| 89 | + }); |
| 90 | + expect(res.message.statusCode).toBe(200); |
| 91 | + let body: string = await res.readBody(); |
| 92 | + let obj:any = JSON.parse(body); |
| 93 | + expect(obj.headers.Accept).toBe('application/json'); |
| 94 | + expect(obj.headers['Content-Type']).toBe('application/x-www-form-urlencoded'); |
| 95 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 96 | + }); |
| 97 | + |
| 98 | + it('pipes a get request', () => { |
| 99 | + return new Promise<string>(async (resolve, reject) => { |
| 100 | + let file: NodeJS.WritableStream = fs.createWriteStream(sampleFilePath); |
| 101 | + (await _http.get('https://httpbin.org/get')).message.pipe(file).on('close', () => { |
| 102 | + let body: string = fs.readFileSync(sampleFilePath).toString(); |
| 103 | + let obj:any = JSON.parse(body); |
| 104 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 105 | + resolve(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('does basic get request with redirects', async() => { |
| 111 | + let res: httpm.HttpClientResponse = await _http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/get")) |
| 112 | + expect(res.message.statusCode).toBe(200); |
| 113 | + let body: string = await res.readBody(); |
| 114 | + let obj:any = JSON.parse(body); |
| 115 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 116 | + }); |
| 117 | + |
| 118 | + it('does basic get request with redirects (303)', async() => { |
| 119 | + let res: httpm.HttpClientResponse = await _http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/get") + '&status_code=303') |
| 120 | + expect(res.message.statusCode).toBe(200); |
| 121 | + let body: string = await res.readBody(); |
| 122 | + let obj:any = JSON.parse(body); |
| 123 | + expect(obj.url).toBe("https://httpbin.org/get"); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns 404 for not found get request on redirect', async() => { |
| 127 | + let res: httpm.HttpClientResponse = await _http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/status/404") + '&status_code=303') |
| 128 | + expect(res.message.statusCode).toBe(404); |
| 129 | + let body: string = await res.readBody(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('does not follow redirects if disabled', async() => { |
| 133 | + let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests', null, { allowRedirects: false }); |
| 134 | + let res: httpm.HttpClientResponse = await http.get("https://httpbin.org/redirect-to?url=" + encodeURIComponent("https://httpbin.org/get")) |
| 135 | + expect(res.message.statusCode).toBe(302); |
| 136 | + let body: string = await res.readBody(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('does basic head request', async() => { |
| 140 | + let res: httpm.HttpClientResponse = await _http.head('http://httpbin.org/get'); |
| 141 | + expect(res.message.statusCode).toBe(200); |
| 142 | + }); |
| 143 | + |
| 144 | + it('does basic http delete request', async() => { |
| 145 | + let res: httpm.HttpClientResponse = await _http.del('http://httpbin.org/delete'); |
| 146 | + expect(res.message.statusCode).toBe(200); |
| 147 | + let body: string = await res.readBody(); |
| 148 | + let obj:any = JSON.parse(body); |
| 149 | + }); |
| 150 | + |
| 151 | + it('does basic http post request', async() => { |
| 152 | + let b: string = 'Hello World!'; |
| 153 | + let res: httpm.HttpClientResponse = await _http.post('http://httpbin.org/post', b); |
| 154 | + expect(res.message.statusCode).toBe(200); |
| 155 | + let body: string = await res.readBody(); |
| 156 | + let obj:any = JSON.parse(body); |
| 157 | + expect(obj.data).toBe(b); |
| 158 | + expect(obj.url).toBe("https://httpbin.org/post"); |
| 159 | + }); |
| 160 | + |
| 161 | + it('does basic http patch request', async() => { |
| 162 | + let b: string = 'Hello World!'; |
| 163 | + let res: httpm.HttpClientResponse = await _http.patch('http://httpbin.org/patch', b); |
| 164 | + expect(res.message.statusCode).toBe(200); |
| 165 | + let body: string = await res.readBody(); |
| 166 | + let obj:any = JSON.parse(body); |
| 167 | + expect(obj.data).toBe(b); |
| 168 | + expect(obj.url).toBe("https://httpbin.org/patch"); |
| 169 | + }); |
| 170 | + |
| 171 | + it('does basic http options request', async() => { |
| 172 | + let res: httpm.HttpClientResponse = await _http.options('http://httpbin.org'); |
| 173 | + expect(res.message.statusCode).toBe(200); |
| 174 | + let body: string = await res.readBody(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('returns 404 for not found get request', async() => { |
| 178 | + let res: httpm.HttpClientResponse = await _http.get('http://httpbin.org/status/404'); |
| 179 | + expect(res.message.statusCode).toBe(404); |
| 180 | + let body: string = await res.readBody(); |
| 181 | + }); |
| 182 | +}) |
0 commit comments