@@ -45,7 +45,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
4545 } ) ;
4646
4747 describe ( "V1" , ( ) => {
48- const emitter = new HTTPEmitter ( ) ;
48+ const emitter = new HTTPEmitter ( { url : receiver } ) ;
4949 const event = new CloudEvent ( V1Spec )
5050 . type ( type )
5151 . source ( source )
@@ -55,7 +55,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
5555 . addExtension ( ext2Name , ext2Value ) ;
5656
5757 it ( "Sends a binary 1.0 CloudEvent by default" , ( ) => {
58- emitter . send ( { url : receiver } , event )
58+ emitter . send ( event )
5959 . then ( ( response ) => {
6060 // A binary message will have a ce-id header
6161 expect ( response . data [ BINARY_HEADERS_1 . ID ] ) . to . equal ( event . getId ( ) ) ;
@@ -65,8 +65,36 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
6565 } ) . catch ( expect . fail ) ;
6666 } ) ;
6767
68- it ( "Sends a structured 1.0 CloudEvent if created that way" , ( ) => {
69- emitter . send ( { url : receiver } , event , "structured" )
68+ it ( "Sends a structured 1.0 CloudEvent if specified" , ( ) => {
69+ emitter . send ( event , { mode : "structured" } )
70+ . then ( ( response ) => {
71+ // A structured message will have a cloud event content type
72+ expect ( response . data [ "content-type" ] ) . to . equal ( DEFAULT_CE_CONTENT_TYPE ) ;
73+ // Ensure other CE headers don't exist - just testing for ID
74+ expect ( response . data [ BINARY_HEADERS_1 . ID ] ) . to . equal ( undefined ) ;
75+ // The spec version would have been specified in the body
76+ expect ( response . data . specversion ) . to . equal ( SPEC_V1 ) ;
77+ expect ( response . data . data . lunchBreak ) . to . equal ( data . lunchBreak ) ;
78+ } ) . catch ( expect . fail ) ;
79+ } ) ;
80+
81+ it ( "Sends to an alternate URL if specified" , ( ) => {
82+ nock ( receiver )
83+ . post ( "/alternate" )
84+ . reply ( function ( uri , requestBody ) {
85+ // return the request body and the headers so they can be
86+ // examined in the test
87+ if ( typeof requestBody === "string" ) {
88+ requestBody = JSON . parse ( requestBody ) ;
89+ }
90+ const returnBody = { ...requestBody , ...this . req . headers } ;
91+ return [
92+ 201 ,
93+ returnBody
94+ ] ;
95+ } ) ;
96+
97+ emitter . send ( event , { mode : "structured" , url : `${ receiver } alternate` } )
7098 . then ( ( response ) => {
7199 // A structured message will have a cloud event content type
72100 expect ( response . data [ "content-type" ] ) . to . equal ( DEFAULT_CE_CONTENT_TYPE ) ;
@@ -80,7 +108,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
80108 } ) ;
81109
82110 describe ( "V03" , ( ) => {
83- const emitter = new HTTPEmitter ( SPEC_V03 ) ;
111+ const emitter = new HTTPEmitter ( { url : receiver , version : SPEC_V03 } ) ;
84112 const event = new CloudEvent ( V03Spec )
85113 . type ( type )
86114 . source ( source )
@@ -90,7 +118,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
90118 . addExtension ( ext2Name , ext2Value ) ;
91119
92120 it ( "Sends a binary 0.3 CloudEvent" , ( ) => {
93- emitter . send ( { url : receiver } , event )
121+ emitter . send ( event )
94122 . then ( ( response ) => {
95123 // A binary message will have a ce-id header
96124 expect ( response . data [ BINARY_HEADERS_03 . ID ] ) . to . equal ( event . getId ( ) ) ;
@@ -100,8 +128,35 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
100128 } ) . catch ( expect . fail ) ;
101129 } ) ;
102130
103- it ( "Sends a structured 0.3 CloudEvent" , ( ) => {
104- emitter . send ( { url : receiver } , event , "structured" )
131+ it ( "Sends a structured 0.3 CloudEvent if specified" , ( ) => {
132+ emitter . send ( event , { mode : "structured" , foo : "bar" } )
133+ . then ( ( response ) => {
134+ // A structured message will have a cloud event content type
135+ expect ( response . data [ "content-type" ] ) . to . equal ( DEFAULT_CE_CONTENT_TYPE ) ;
136+ // Ensure other CE headers don't exist - just testing for ID
137+ expect ( response . data [ BINARY_HEADERS_03 . ID ] ) . to . equal ( undefined ) ;
138+ // The spec version would have been specified in the body
139+ expect ( response . data . specversion ) . to . equal ( SPEC_V03 ) ;
140+ expect ( response . data . data . lunchBreak ) . to . equal ( data . lunchBreak ) ;
141+ } ) . catch ( expect . fail ) ;
142+ } ) ;
143+ it ( "Sends to an alternate URL if specified" , ( ) => {
144+ nock ( receiver )
145+ . post ( "/alternate" )
146+ . reply ( function ( uri , requestBody ) {
147+ // return the request body and the headers so they can be
148+ // examined in the test
149+ if ( typeof requestBody === "string" ) {
150+ requestBody = JSON . parse ( requestBody ) ;
151+ }
152+ const returnBody = { ...requestBody , ...this . req . headers } ;
153+ return [
154+ 201 ,
155+ returnBody
156+ ] ;
157+ } ) ;
158+
159+ emitter . send ( event , { mode : "structured" , url : `${ receiver } alternate` } )
105160 . then ( ( response ) => {
106161 // A structured message will have a cloud event content type
107162 expect ( response . data [ "content-type" ] ) . to . equal ( DEFAULT_CE_CONTENT_TYPE ) ;
0 commit comments