File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ var axios = require ( "axios" ) ;
2
+
3
+ function HTTPStructured ( configuration ) {
4
+ this . config = configuration ;
5
+
6
+ this . config [ 'headers' ] = {
7
+ 'Content-Type' :'application/cloudevents+json; charset=utf-8'
8
+ } ;
9
+ }
10
+
11
+ HTTPStructured . prototype . emit = function ( cloudevent ) {
12
+
13
+ // Create new request object
14
+ var _config = JSON . parse ( JSON . stringify ( this . config ) ) ;
15
+
16
+ // Set the cloudevent payload
17
+ _config [ 'data' ] = cloudevent . format ( ) ;
18
+
19
+ // Return the Promise
20
+ return axios . request ( _config ) ;
21
+ }
22
+
23
+ module . exports = HTTPStructured ;
24
+
Original file line number Diff line number Diff line change
1
+ var expect = require ( "chai" ) . expect ;
2
+ var Cloudevent = require ( "../index.js" ) ;
3
+ var nock = require ( "nock" ) ;
4
+
5
+ const type = "com.github.pull.create" ;
6
+ const source = "urn:event:from:myapi/resourse/123" ;
7
+ const webhook = "https://cloudevents.io/webhook" ;
8
+ const contentType = "application/cloudevents+json; charset=utf-8" ;
9
+
10
+ var cloudevent = new Cloudevent ( )
11
+ . type ( type )
12
+ . source ( source ) ;
13
+
14
+ var httpcfg = {
15
+ method : 'POST' ,
16
+ url : webhook + '/json'
17
+ } ;
18
+
19
+ var httpstructured_0_1 =
20
+ new Cloudevent . bindings [ 'http-structured0.1' ] ( httpcfg ) ;
21
+
22
+ describe ( "HTTP Transport Binding" , ( ) => {
23
+ beforeEach ( ( ) => {
24
+ // Mocking the webhook
25
+ nock ( webhook )
26
+ . post ( "/json" )
27
+ . reply ( 201 , { status : 'accepted' } ) ;
28
+ } ) ;
29
+
30
+ describe ( "Structured" , ( ) => {
31
+ describe ( "JSON Format" , ( ) => {
32
+ it ( "requires '" + contentType + "' Content-Type in header" , ( ) => {
33
+ return httpstructured_0_1 . emit ( cloudevent )
34
+ . then ( response => {
35
+ //console.log(response.config);
36
+ expect ( response . config . headers [ 'Content-Type' ] )
37
+ . to . equal ( contentType ) ;
38
+ } ) ;
39
+ } ) ;
40
+ } ) ;
41
+ } ) ;
42
+ } ) ;
43
+
You can’t perform that action at this time.
0 commit comments