You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					64 lines
				
				1.5 KiB
			
		
		
			
		
	
	
					64 lines
				
				1.5 KiB
			| 
											3 years ago
										 | 'use strict' | ||
|  | 
 | ||
|  | function asyncAwaitTest (t, inject) { | ||
|  |   t.plan(4) | ||
|  | 
 | ||
|  |   t.test('basic async await', async t => { | ||
|  |     const dispatch = function (req, res) { | ||
|  |       res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
|  |       res.end('hello') | ||
|  |     } | ||
|  | 
 | ||
|  |     try { | ||
|  |       const res = await inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }) | ||
|  |       t.equal(res.payload, 'hello') | ||
|  |     } catch (err) { | ||
|  |       t.fail(err) | ||
|  |     } | ||
|  |   }) | ||
|  | 
 | ||
|  |   t.test('basic async await (errored)', async t => { | ||
|  |     const dispatch = function (req, res) { | ||
|  |       res.connection.destroy(new Error('kaboom')) | ||
|  |     } | ||
|  | 
 | ||
|  |     try { | ||
|  |       await inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }) | ||
|  |       t.fail('should throw') | ||
|  |     } catch (err) { | ||
|  |       t.ok(err) | ||
|  |     } | ||
|  |   }) | ||
|  | 
 | ||
|  |   t.test('chainable api with async await', async t => { | ||
|  |     const dispatch = function (req, res) { | ||
|  |       res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
|  |       res.end('hello') | ||
|  |     } | ||
|  | 
 | ||
|  |     try { | ||
|  |       const chain = inject(dispatch).get('http://example.com:8080/hello') | ||
|  |       const res = await chain.end() | ||
|  |       t.equal(res.payload, 'hello') | ||
|  |     } catch (err) { | ||
|  |       t.fail(err) | ||
|  |     } | ||
|  |   }) | ||
|  | 
 | ||
|  |   t.test('chainable api with async await without end()', async t => { | ||
|  |     const dispatch = function (req, res) { | ||
|  |       res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
|  |       res.end('hello') | ||
|  |     } | ||
|  | 
 | ||
|  |     try { | ||
|  |       const res = await inject(dispatch).get('http://example.com:8080/hello') | ||
|  |       t.equal(res.payload, 'hello') | ||
|  |     } catch (err) { | ||
|  |       t.fail(err) | ||
|  |     } | ||
|  |   }) | ||
|  | } | ||
|  | 
 | ||
|  | module.exports = asyncAwaitTest |