|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const Buffer = require('safe-buffer').Buffer |
| 4 | + |
3 | 5 | const npmlog = require('npmlog')
|
| 6 | +const PassThrough = require('stream').PassThrough |
4 | 7 | const test = require('tap').test
|
5 | 8 | const tnock = require('./util/tnock.js')
|
6 | 9 |
|
@@ -34,17 +37,115 @@ test('hello world', t => {
|
34 | 37 | .get('/hello')
|
35 | 38 | .reply(200, {hello: 'world'})
|
36 | 39 | return fetch('/hello', OPTS)
|
| 40 | + .then(res => { |
| 41 | + t.equal(res.status, 200, 'got successful response') |
| 42 | + return res.json() |
| 43 | + }) |
| 44 | + .then(json => t.deepEqual(json, {hello: 'world'}, 'got correct body')) |
| 45 | +}) |
| 46 | + |
| 47 | +test('JSON body param', t => { |
| 48 | + tnock(t, OPTS.config.get('registry')) |
| 49 | + .matchHeader('content-type', ctype => { |
| 50 | + t.equal(ctype[0], 'application/json', 'content-type automatically set') |
| 51 | + return ctype[0] === 'application/json' |
| 52 | + }) |
| 53 | + .post('/hello') |
| 54 | + .reply(200, (uri, reqBody) => { |
| 55 | + t.deepEqual(reqBody, { |
| 56 | + hello: 'world' |
| 57 | + }, 'got the JSON version of the body') |
| 58 | + return reqBody |
| 59 | + }) |
| 60 | + const opts = Object.assign({ |
| 61 | + method: 'POST', |
| 62 | + body: {hello: 'world'} |
| 63 | + }, OPTS) |
| 64 | + return fetch('/hello', opts) |
37 | 65 | .then(res => {
|
38 | 66 | t.equal(res.status, 200)
|
39 | 67 | return res.json()
|
40 | 68 | })
|
41 | 69 | .then(json => t.deepEqual(json, {hello: 'world'}))
|
42 | 70 | })
|
43 | 71 |
|
44 |
| -test('json()') |
45 |
| -test('method configurable') |
46 |
| -test('npm-notice header logging') |
47 |
| -test('optionally verifies request body integrity') |
| 72 | +test('buffer body param', t => { |
| 73 | + tnock(t, OPTS.config.get('registry')) |
| 74 | + .matchHeader('content-type', ctype => { |
| 75 | + t.equal(ctype[0], 'application/octet-stream', 'content-type automatically set') |
| 76 | + return ctype[0] === 'application/octet-stream' |
| 77 | + }) |
| 78 | + .post('/hello') |
| 79 | + .reply(200, (uri, reqBody) => { |
| 80 | + t.deepEqual( |
| 81 | + Buffer.from(reqBody, 'utf8'), |
| 82 | + Buffer.from('hello', 'utf8'), |
| 83 | + 'got the JSON version of the body' |
| 84 | + ) |
| 85 | + return reqBody |
| 86 | + }) |
| 87 | + const opts = Object.assign({ |
| 88 | + method: 'POST', |
| 89 | + body: Buffer.from('hello', 'utf8') |
| 90 | + }, OPTS) |
| 91 | + return fetch('/hello', opts) |
| 92 | + .then(res => { |
| 93 | + t.equal(res.status, 200) |
| 94 | + return res.buffer() |
| 95 | + }) |
| 96 | + .then(buf => |
| 97 | + t.deepEqual(buf, Buffer.from('hello', 'utf8'), 'got response') |
| 98 | + ) |
| 99 | +}) |
| 100 | + |
| 101 | +test('stream body param', t => { |
| 102 | + tnock(t, OPTS.config.get('registry')) |
| 103 | + .matchHeader('content-type', ctype => { |
| 104 | + t.equal(ctype[0], 'application/octet-stream', 'content-type automatically set') |
| 105 | + return ctype[0] === 'application/octet-stream' |
| 106 | + }) |
| 107 | + .post('/hello') |
| 108 | + .reply(200, (uri, reqBody) => { |
| 109 | + t.deepEqual(JSON.parse(reqBody), { |
| 110 | + hello: 'world' |
| 111 | + }, 'got the stringified version of the body') |
| 112 | + return reqBody |
| 113 | + }) |
| 114 | + const stream = new PassThrough() |
| 115 | + setImmediate(() => stream.end(JSON.stringify({hello: 'world'}))) |
| 116 | + const opts = Object.assign({ |
| 117 | + method: 'POST', |
| 118 | + body: stream |
| 119 | + }, OPTS) |
| 120 | + return fetch('/hello', opts) |
| 121 | + .then(res => { |
| 122 | + t.equal(res.status, 200) |
| 123 | + return res.json() |
| 124 | + }) |
| 125 | + .then(json => t.deepEqual(json, {hello: 'world'})) |
| 126 | +}) |
| 127 | + |
| 128 | +test('json()', t => { |
| 129 | + tnock(t, OPTS.config.get('registry')) |
| 130 | + .get('/hello') |
| 131 | + .reply(200, {hello: 'world'}) |
| 132 | + return fetch.json('/hello', OPTS) |
| 133 | + .then(json => t.deepEqual(json, {hello: 'world'}, 'got json body')) |
| 134 | +}) |
| 135 | + |
| 136 | +test('method configurable', t => { |
| 137 | + tnock(t, OPTS.config.get('registry')) |
| 138 | + .delete('/hello') |
| 139 | + .reply(200) |
| 140 | + const opts = Object.assign({ |
| 141 | + method: 'DELETE' |
| 142 | + }, OPTS) |
| 143 | + return fetch('/hello', opts) |
| 144 | + .then(res => { |
| 145 | + t.equal(res.status, 200, 'successfully used DELETE method') |
| 146 | + }) |
| 147 | +}) |
| 148 | + |
48 | 149 | test('pickRegistry() utility')
|
49 | 150 |
|
50 | 151 | // TODO
|
|
0 commit comments