11'use strict'
22
33const http = require ( 'node:http' )
4- const assertRejects = require ( 'assert-rejects' )
54const { expect } = require ( 'chai' )
65const sinon = require ( 'sinon' )
76const nock = require ( '../..' )
@@ -37,12 +36,12 @@ describe('Header matching', () => {
3736 . get ( '/' )
3837 . reply ( 200 , 'Hello World!' )
3938
40- await assertRejects (
41- got ( 'http://example.test/' , {
42- headers : { 'X-My-Headers' : 456 } ,
43- } ) ,
44- / N o c k : N o m a t c h f o r r e q u e s t / ,
45- )
39+ const { statusCode , body } = await got ( 'http://example.test/' , {
40+ headers : { 'X-My-Headers' : 456 } ,
41+ responseType : 'json' ,
42+ } ) . catch ( err => err . response )
43+ expect ( statusCode ) . to . equal ( 501 )
44+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
4645 } )
4746
4847 it ( 'should not consume mock request when match is declined by function' , async ( ) => {
@@ -53,12 +52,12 @@ describe('Header matching', () => {
5352 . get ( '/' )
5453 . reply ( 200 , 'Hello World!' )
5554
56- await assertRejects (
57- got ( 'http://example.test/' , {
58- headers : { '-My-Headers' : 456 } ,
59- } ) ,
60- / N o c k : N o m a t c h f o r r e q u e s t / ,
61- )
55+ const { statusCode , body } = await got ( 'http://example.test/' , {
56+ headers : { '-My-Headers' : 456 } ,
57+ responseType : 'json' ,
58+ } ) . catch ( err => err . response )
59+ expect ( statusCode ) . to . equal ( 501 )
60+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
6261
6362 expect ( scope . isDone ( ) ) . to . be . false ( )
6463 } )
@@ -215,12 +214,12 @@ describe('Header matching', () => {
215214 . matchHeader ( 'x-my-headers' , ( ) => false )
216215 . reply ( 200 , 'Hello World!' )
217216
218- await assertRejects (
219- got ( 'http://example.test/' , {
220- headers : { 'X-My-Headers' : 456 } ,
221- } ) ,
222- / N o c k : N o m a t c h f o r r e q u e s t / ,
223- )
217+ const { statusCode , body } = await got ( 'http://example.test/' , {
218+ headers : { 'X-My-Headers' : 456 } ,
219+ responseType : 'json' ,
220+ } ) . catch ( err => err . response )
221+ expect ( statusCode ) . to . equal ( 501 )
222+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
224223 } )
225224
226225 it ( 'should not consume mock request when match is declined by function' , async ( ) => {
@@ -229,12 +228,12 @@ describe('Header matching', () => {
229228 . matchHeader ( 'x-my-headers' , ( ) => false )
230229 . reply ( 200 , 'Hello World!' )
231230
232- await assertRejects (
233- got ( 'http://example.test/' , {
234- headers : { '-My-Headers' : 456 } ,
235- } ) ,
236- / N o c k : N o m a t c h f o r r e q u e s t / ,
237- )
231+ const { statusCode , body } = await got ( 'http://example.test/' , {
232+ headers : { '-My-Headers' : 456 } ,
233+ responseType : 'json' ,
234+ } ) . catch ( err => err . response )
235+ expect ( statusCode ) . to . equal ( 501 )
236+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
238237
239238 expect ( scope . isDone ( ) ) . to . be . false ( )
240239 } )
@@ -274,12 +273,14 @@ describe('Header matching', () => {
274273 . post ( '/' )
275274 . reply ( 200 , { status : 'ok' } )
276275
277- await assertRejects (
278- got . post ( 'http://example.test/' , {
276+ const { statusCode , body } = await got
277+ . post ( 'http://example.test/' , {
279278 headers : { 'X-App-Token' : 'apptoken' } ,
280- } ) ,
281- / N o c k : N o m a t c h f o r r e q u e s t / ,
282- )
279+ responseType : 'json' ,
280+ } )
281+ . catch ( err => err . response )
282+ expect ( statusCode ) . to . equal ( 501 )
283+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
283284 } )
284285
285286 it ( 'should match when request header matches regular expression' , async ( ) => {
@@ -306,12 +307,14 @@ describe('Header matching', () => {
306307 . post ( '/' )
307308 . reply ( )
308309
309- await assertRejects (
310- got . post ( 'http://example.test/' , {
310+ const { statusCode , body } = await got
311+ . post ( 'http://example.test/' , {
311312 headers : { 'X-My-Super-Power' : 'mullet growing' } ,
312- } ) ,
313- / N o c k : N o m a t c h / ,
314- )
313+ responseType : 'json' ,
314+ } )
315+ . catch ( err => err . response )
316+ expect ( statusCode ) . to . equal ( 501 )
317+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
315318
316319 expect ( scope . isDone ( ) ) . to . be . false ( )
317320 } )
@@ -369,12 +372,14 @@ describe('Header matching', () => {
369372 . post ( '/' )
370373 . reply ( )
371374
372- await assertRejects (
373- got . post ( 'http://example.test/' , {
375+ const { statusCode , body } = await got
376+ . post ( 'http://example.test/' , {
374377 headers : { 'X-My-Super-Power' : 'mullet growing' } ,
375- } ) ,
376- / N o c k : N o m a t c h / ,
377- )
378+ responseType : 'json' ,
379+ } )
380+ . catch ( err => err . response )
381+ expect ( statusCode ) . to . equal ( 501 )
382+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
378383
379384 expect ( scope . isDone ( ) ) . to . be . false ( )
380385 } )
@@ -510,12 +515,12 @@ describe('Header matching', () => {
510515 . get ( '/' )
511516 . reply ( )
512517
513- await assertRejects (
514- got ( 'http://example.test/' , {
515- headers : { Cookie : 'cookie' , Donut : 'donut' } ,
516- } ) ,
517- / N o c k : N o m a t c h f o r r e q u e s t / ,
518- )
518+ const { statusCode , body } = await got ( 'http://example.test/' , {
519+ headers : { Cookie : 'cookie' , Donut : 'donut' } ,
520+ responseType : 'json' ,
521+ } ) . catch ( err => err . response )
522+ expect ( statusCode ) . to . equal ( 501 )
523+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
519524
520525 expect ( scope . isDone ( ) ) . to . be . false ( )
521526 } )
@@ -572,12 +577,12 @@ describe('Header matching', () => {
572577 . get ( '/' )
573578 . reply ( )
574579
575- await assertRejects (
576- got ( 'http://example. test/' , {
577- headers : { Host : 'some.other.domain.test' } ,
578- } ) ,
579- / N o c k : N o m a t c h f o r r e q u e s t / ,
580- )
580+ const { statusCode , body } = await got ( 'http://example.test/' , {
581+ headers : { Host : 'some.other.domain. test' } ,
582+ responseType : 'json' ,
583+ } ) . catch ( err => err . response )
584+ expect ( statusCode ) . to . equal ( 501 )
585+ expect ( body . code ) . to . equal ( 'ERR_NOCK_NO_MATCH' )
581586 } )
582587 } )
583588} )
0 commit comments