@@ -43,4 +43,67 @@ describe("Functional approach", () => {
4343 expect ( actual ) . to . equal ( true ) ;
4444 } ) ;
4545 } ) ;
46+
47+ describe ( "asData" , ( ) => {
48+ it ( "should throw error when data is not a valid json" , ( ) => {
49+ let data = "not a json" ;
50+
51+ expect ( fun . asData . bind ( fun , data , "application/json" ) )
52+ . to
53+ . throws ( ) ;
54+ } ) ;
55+
56+ it ( "should parse string content type as string" , ( ) => {
57+ let expected = "a string" ;
58+
59+ let actual = fun . asData ( expected , "text/plain" ) ;
60+
61+ expect ( ( typeof actual ) ) . to . equal ( "string" ) ;
62+ expect ( actual ) . to . equal ( expected ) ;
63+ } ) ;
64+
65+ it ( "should parse 'application/json' as json object" , ( ) => {
66+ let expected = {
67+ much : "wow" ,
68+ myext : {
69+ ext : "x04"
70+ }
71+ } ;
72+
73+ let actual = fun . asData ( JSON . stringify ( expected ) , "application/json" ) ;
74+
75+ expect ( ( typeof actual ) ) . to . equal ( "object" ) ;
76+ expect ( actual ) . to . deep . equal ( expected ) ;
77+ } ) ;
78+
79+ it ( "should parse 'application/cloudevents+json' as json object" , ( ) => {
80+ let expected = {
81+ much : "wow" ,
82+ myext : {
83+ ext : "x04"
84+ }
85+ } ;
86+
87+ let actual = fun . asData ( JSON . stringify ( expected ) ,
88+ "application/cloudevents+json" ) ;
89+
90+ expect ( ( typeof actual ) ) . to . equal ( "object" ) ;
91+ expect ( actual ) . to . deep . equal ( expected ) ;
92+ } ) ;
93+
94+ it ( "should parse 'text/json' as json object" , ( ) => {
95+ let expected = {
96+ much : "wow" ,
97+ myext : {
98+ ext : "x04"
99+ }
100+ } ;
101+
102+ let actual = fun . asData ( JSON . stringify ( expected ) ,
103+ "text/json" ) ;
104+
105+ expect ( ( typeof actual ) ) . to . equal ( "object" ) ;
106+ expect ( actual ) . to . deep . equal ( expected ) ;
107+ } ) ;
108+ } ) ;
46109} ) ;
0 commit comments