1+ // tslint:disable:no-any
2+
13import {
24 mesh ,
35 Mesh ,
@@ -29,37 +31,56 @@ describe('http-support', () => {
2931 it ( 'can send messages over http' , async ( ) => {
3032 await p1 . subject ( 'test-sub-2' )
3133 // tslint:disable-next-line:no-any
32- . on ( 'test-http' , ( msg : any ) => {
33- return { echo : msg } ;
34+ . on ( 'test-http' , ( msg : any , header : SubjectMessageHeader ) => {
35+ return {
36+ echo : {
37+ msg,
38+ header
39+ }
40+ } ;
3441 } )
3542 . awaitRegistration ( ) ;
36- const res : Response = await fetch ( `http://localhost:${ port } /api/test-sub-2/123` ,
37- {
38- headers : {
39- 'content-type' : 'application/json'
40- } ,
41- method : 'post' ,
42- body : JSON . stringify ( { name : 'test-http' } )
43- } ) ;
43+ const res : Response = await sendHttp ( `${ port } /api/test-sub-2/123` , { name : 'test-http' } ) ;
44+ expect ( res . status )
45+ . toBe ( 200 ) ;
4446 const resJson = await res . json ( ) ;
45- expect ( resJson . echo . name )
47+ expect ( resJson . echo . msg . name )
48+ . toBe ( 'test-http' ) ;
49+ expect ( resJson . echo . header . http . headers [ 'content-type' ] )
50+ . toBe ( 'application/json' ) ;
51+ const resDirect : any = await p1 . subject ( 'test-sub-2' )
52+ . send ( { name :'test-http' } ) ;
53+ expect ( resDirect . echo . msg . name )
4654 . toBe ( 'test-http' ) ;
4755 } ) ;
56+ it ( 'can set the http response status in the handler' , async ( ) => {
57+ await p1 . subject ( 'test-sub-2' )
58+ // tslint:disable-next-line:no-any
59+ . on ( 'test-http' , ( ) => {
60+ return {
61+ http : {
62+ status : 202 ,
63+ headers : {
64+ 'x-something' : 'foo'
65+ }
66+ }
67+ } ;
68+ } )
69+ . awaitRegistration ( ) ;
70+ const res : Response = await sendHttp ( `${ port } /api/test-sub-2/123` , { name : 'test-http' } ) ;
71+ expect ( res . status )
72+ . toBe ( 202 ) ;
73+ expect ( res . headers . get ( 'x-something' ) )
74+ . toBe ( 'foo' ) ;
75+ } ) ;
4876 it ( 'returns status 404 if there are no handlers' , async ( ) => {
4977 await p1 . subject ( 'test-sub-2' )
5078 // tslint:disable-next-line:no-any
5179 . on ( 'test-http' , ( msg : any ) => {
5280 return { echo : msg } ;
5381 } )
5482 . awaitRegistration ( ) ;
55- let res : Response = await fetch ( `http://localhost:${ port } /api/no-sub/123` ,
56- {
57- headers : {
58- 'content-type' : 'application/json'
59- } ,
60- method : 'post' ,
61- body : JSON . stringify ( { name : 'test-http' } )
62- } ) ;
83+ let res : Response = await sendHttp ( `${ port } /api/no-sub/123` , { name : 'test-http' } ) ;
6384 expect ( res . status )
6485 . toBe ( 404 ) ;
6586 res = await fetch ( `http://localhost:${ port } /api/broadcast/no-sub` ,
@@ -80,14 +101,7 @@ describe('http-support', () => {
80101 return { echo : msg } ;
81102 } )
82103 . awaitRegistration ( ) ;
83- const res : Response = await fetch ( `http://localhost:${ port } /api/test-sub-2/123?messageName=test-http` ,
84- {
85- headers : {
86- 'content-type' : 'application/json'
87- } ,
88- method : 'post' ,
89- body : '{}'
90- } ) ;
104+ const res : Response = await sendHttp ( `${ port } /api/test-sub-2/123?messageName=test-http` , { } ) ;
91105 const resJson = await res . json ( ) ;
92106 expect ( resJson . echo . name )
93107 . toBe ( 'test-http' ) ;
@@ -99,14 +113,7 @@ describe('http-support', () => {
99113 return { echo : msg } ;
100114 } )
101115 . awaitRegistration ( ) ;
102- const res : Response = await fetch ( `http://localhost:${ port } /api/broadcast/test-sub-2?messageName=test-http` ,
103- {
104- headers : {
105- 'content-type' : 'application/json'
106- } ,
107- method : 'post' ,
108- body : '{}'
109- } ) ;
116+ const res : Response = await sendHttp ( `${ port } /api/broadcast/test-sub-2?messageName=test-http` , { } ) ;
110117 const resJson = await res . json ( ) ;
111118 expect ( resJson . length )
112119 . toBe ( 1 ) ;
@@ -120,14 +127,7 @@ describe('http-support', () => {
120127 return { echo : msg } ;
121128 } )
122129 . awaitRegistration ( ) ;
123- const res : Response = await fetch ( `http://localhost:${ port } /api/broadcast/test-sub-2?messageName=test-http&wait=false` ,
124- {
125- headers : {
126- 'content-type' : 'application/json'
127- } ,
128- method : 'post' ,
129- body : '{}'
130- } ) ;
130+ const res : Response = await sendHttp ( `${ port } /api/broadcast/test-sub-2?messageName=test-http&wait=false` , { } ) ;
131131 const resJson = await res . json ( ) ;
132132 expect ( resJson . length )
133133 . toBe ( 0 ) ;
@@ -141,14 +141,10 @@ describe('http-support', () => {
141141 return { name : header . name , pk : header . partitionKey } ;
142142 } )
143143 . awaitRegistration ( ) ;
144- const res : Response = await fetch ( `http://localhost:${ port } /api/test-sub-2/{body.key}-123?messageName={body.something}` ,
145- {
146- headers : {
147- 'content-type' : 'application/json'
148- } ,
149- method : 'post' ,
150- body : JSON . stringify ( { key : 'abc' , something : 'some-val' } )
151- } ) ;
144+ const res : Response = await sendHttp ( `${ port } /api/test-sub-2/{body.key}-123?messageName={body.something}` , {
145+ key : 'abc' ,
146+ something : 'some-val'
147+ } ) ;
152148 const resJson = await res . json ( ) ;
153149 expect ( resJson . name )
154150 . toBe ( 'some-val' ) ;
@@ -162,15 +158,19 @@ describe('http-support', () => {
162158 return { echo : msg } ;
163159 } )
164160 . awaitRegistration ( ) ;
165- const res : Response = await fetch ( `http://localhost:${ port } /api/test-sub-2/123` ,
166- {
167- headers : {
168- 'content-type' : 'application/json'
169- } ,
170- method : 'post' ,
171- body : '{}'
172- } ) ;
161+ const res : Response = await sendHttp ( `${ port } /api/test-sub-2/123` , { } ) ;
173162 expect ( res . status )
174163 . toBe ( 400 ) ;
175164 } ) ;
176165} ) ;
166+
167+ function sendHttp ( path : string , body : any ) {
168+ return fetch ( `http://localhost:${ path } ` ,
169+ {
170+ headers : {
171+ 'content-type' : 'application/json'
172+ } ,
173+ method : 'post' ,
174+ body : JSON . stringify ( body )
175+ } ) ;
176+ }
0 commit comments