@@ -5,67 +5,58 @@ var test = require('tap').test;
55var mikealRequest = require ( 'request' ) ;
66var assert = require ( 'assert' ) ;
77
8- test ( 'match body with regex' , function ( t ) {
9-
8+ var testBodyMatch = function ( expected , actual , shouldMatch , t ) {
109 nock ( 'http://encodingsareus.com' )
11- . post ( '/' , { auth : { passwd : / a . + / } } )
10+ . post ( '/' , expected )
1211 . reply ( 200 ) ;
1312
1413 mikealRequest ( {
1514 url : 'http://encodingsareus.com/' ,
1615 method : 'post' ,
17- json : {
18- auth : {
19- passwd : 'abc'
20- }
21- } ,
16+ json : actual ,
2217 } , function ( err , res ) {
23- if ( err ) throw err ;
24- assert . equal ( res . statusCode , 200 ) ;
25- t . end ( ) ;
18+ shouldMatch
19+ ? assert . equal ( res . statusCode , 200 )
20+ : assert ( err && / N o m a t c h f o r r e q u e s t / . test ( err . message ) , 'nock should not intercept the request' ) ;
21+ t . end ( ) ;
2622 } ) ;
23+ }
2724
28- } ) ;
2925
30- test ( 'match body with regex inside array' , function ( t ) {
26+ test ( 'body matching' , function ( t ) {
27+ t . test ( 'match body with regex' , function ( t2 ) {
28+ testBodyMatch ( { auth : { passwd : / a .+ / } } , { auth : { passwd : 'abc' } } , true , t2 ) ;
29+ } ) ;
3130
32- nock ( 'http://encodingsareus.com' )
33- . post ( '/' , { items : [ { name : / t .+ / } ] } )
34- . reply ( 200 ) ;
31+ t . test ( 'match body with regex inside array' , function ( t2 ) {
32+ testBodyMatch ( { items : [ { name : / t .+ / } ] } , { items : [ { name : 'test' } ] } , true , t2 ) ;
33+ } ) ;
3534
36- mikealRequest ( {
37- url : 'http://encodingsareus.com/' ,
38- method : 'post' ,
39- json : {
40- items : [ {
41- name : 'test'
42- } ]
43- } ,
44- } , function ( err , res ) {
45- if ( err ) throw err ;
46- assert . equal ( res . statusCode , 200 ) ;
47- t . end ( ) ;
35+ t . test ( 'failing body matching with regex inside array' , function ( t2 ) {
36+ testBodyMatch ( { items : [ { name : / o .+ / } ] } , { items : [ { name : 'test' } ] } , false , t2 ) ;
4837 } ) ;
49- } )
5038
51- test ( 'match body with empty object inside' , function ( t ) {
39+ t . test ( 'failing body matching with different array length' , function ( t2 ) {
40+ testBodyMatch ( { items : [ 1 , 2 ] } , { items : [ { items : [ 1 , 2 , 3 ] } ] } , false , t2 ) ;
41+ } ) ;
5242
53- nock ( 'http://encodingsareus.com' )
54- . post ( '/' , { obj : { } } )
55- . reply ( 200 ) ;
43+ t . test ( 'match body with empty object inside' , function ( t2 ) {
44+ testBodyMatch ( { obj : { } } , { obj : { } } , true , t2 ) ;
45+ } ) ;
5646
57- mikealRequest ( {
58- url : 'http://encodingsareus.com/' ,
59- method : 'post' ,
60- json : {
61- obj : { }
62- } ,
63- } , function ( err , res ) {
64- if ( err ) throw err ;
65- assert . equal ( res . statusCode , 200 ) ;
66- t . end ( ) ;
47+ t . test ( 'match body with identical objects' , function ( t2 ) {
48+ testBodyMatch ( { obj : { b : 1 , a : 1 } } , { obj : { b : 1 , a : 1 } } , true , t2 ) ;
49+ } ) ;
50+
51+ t . test ( 'failing body matching with different objects' , function ( t2 ) {
52+ testBodyMatch ( { obj : { a : 1 } } , { obj : { b : 1 , a : 1 } } , false , t2 ) ;
53+ } ) ;
54+
55+ t . test ( 'failing body matching with different nested objects' , function ( t2 ) {
56+ testBodyMatch ( { obj : { a : { b : 1 } } } , { obj : { a : { b : 1 , c : 1 } } } , false , t2 ) ;
6757 } ) ;
68- } )
58+ t . end ( ) ;
59+ } ) ;
6960
7061test ( 'match body with form multipart' , function ( t ) {
7162
0 commit comments