@@ -2,6 +2,7 @@ const expect = require("chai").expect;
2
2
const nock = require ( "nock" ) ;
3
3
const http = require ( "http" ) ;
4
4
const request = require ( "request" ) ;
5
+ const { asBase64} = require ( "../lib/utils/fun.js" ) ;
5
6
6
7
const BinaryHTTPEmitter =
7
8
require ( "../lib/bindings/http/emitter_binary_1.js" ) ;
@@ -38,13 +39,16 @@ const cloudevent =
38
39
. addExtension ( ext1Name , ext1Value )
39
40
. addExtension ( ext2Name , ext2Value ) ;
40
41
42
+ const dataString = ")(*~^my data for ce#@#$%"
43
+
41
44
const webhook = "https://cloudevents.io/webhook/v1" ;
42
45
const httpcfg = {
43
46
method : "POST" ,
44
47
url : webhook + "/json"
45
48
} ;
46
49
47
50
const binary = new BinaryHTTPEmitter ( httpcfg ) ;
51
+ const structured = new v1 . StructuredHTTPEmitter ( httpcfg ) ;
48
52
49
53
describe ( "HTTP Transport Binding - Version 1.0" , ( ) => {
50
54
beforeEach ( ( ) => {
@@ -54,6 +58,65 @@ describe("HTTP Transport Binding - Version 1.0", () => {
54
58
. reply ( 201 , { status : "accepted" } ) ;
55
59
} ) ;
56
60
61
+ describe ( "Structured" , ( ) => {
62
+ describe ( "JSON Format" , ( ) => {
63
+ it ( "requires '" + contentType + "' Content-Type in the header" , ( ) => {
64
+ return structured . emit ( cloudevent )
65
+ . then ( ( response ) => {
66
+ expect ( response . config . headers [ "Content-Type" ] )
67
+ . to . equal ( contentType ) ;
68
+ } ) ;
69
+ } ) ;
70
+
71
+ it ( "the request payload should be correct" , ( ) => {
72
+ return structured . emit ( cloudevent )
73
+ . then ( ( response ) => {
74
+ expect ( JSON . parse ( response . config . data ) )
75
+ . to . deep . equal ( cloudevent . format ( ) ) ;
76
+ } ) ;
77
+ } ) ;
78
+
79
+ describe ( "Binary event data" , ( ) => {
80
+ it ( "the request payload should be correct when data is binary" , ( ) => {
81
+ let bindata = Uint32Array . from ( dataString , ( c ) => c . codePointAt ( 0 ) ) ;
82
+ let expected = asBase64 ( bindata ) ;
83
+ let binevent =
84
+ new Cloudevent ( v1 . Spec )
85
+ . type ( type )
86
+ . source ( source )
87
+ . dataContentType ( "text/plain" )
88
+ . data ( bindata )
89
+ . addExtension ( ext1Name , ext1Value )
90
+ . addExtension ( ext2Name , ext2Value ) ;
91
+
92
+ return structured . emit ( binevent )
93
+ . then ( ( response ) => {
94
+ expect ( JSON . parse ( response . config . data ) . data_base64 )
95
+ . to . equal ( expected ) ;
96
+ } ) ;
97
+ } ) ;
98
+
99
+ it ( "the payload must have 'data_base64' when data is binary" , ( ) => {
100
+ let binevent =
101
+ new Cloudevent ( v1 . Spec )
102
+ . type ( type )
103
+ . source ( source )
104
+ . dataContentType ( "text/plain" )
105
+ . data ( Uint32Array . from ( dataString , ( c ) => c . codePointAt ( 0 ) ) )
106
+ . addExtension ( ext1Name , ext1Value )
107
+ . addExtension ( ext2Name , ext2Value ) ;
108
+
109
+ return structured . emit ( binevent )
110
+ . then ( ( response ) => {
111
+ console . log ( response . config . data ) ;
112
+ expect ( JSON . parse ( response . config . data ) )
113
+ . to . have . property ( "data_base64" ) ;
114
+ } ) ;
115
+ } ) ;
116
+ } ) ;
117
+ } ) ;
118
+ } ) ;
119
+
57
120
describe ( "Binary" , ( ) => {
58
121
describe ( "JSON Format" , ( ) => {
59
122
it ( "requires '" + cloudevent . getDataContentType ( ) + "' Content-Type in the header" , ( ) => {
0 commit comments