3
3
var config = process . argv [ process . argv . length - 1 ]
4
4
config = ( config . indexOf ( '--config=' ) !== - 1 ) ? config . substr ( 9 ) : 'Debug' ;
5
5
6
- var functions = require ( '../../bin/' + config + '/Content/Script/functions.js' ) ;
6
+ function testRequire ( script ) {
7
+ return require ( '../../bin/' + config + '/Content/Script/' + script ) ;
8
+ }
7
9
8
- var context = { } ;
9
- var logs = [ ] ;
10
+ var functions = testRequire ( 'functions.js' ) ;
11
+ var response = testRequire ( 'http/response.js' ) ;
12
+ var request = testRequire ( 'http/request.js' ) ;
13
+
14
+ describe ( 'http' , ( ) => {
15
+ describe ( 'response' , ( ) => {
16
+ var res , context ;
17
+ beforeEach ( ( ) => {
18
+ context = {
19
+ isDone : false ,
20
+ done : ( ) => context . isDone = true
21
+ } ;
22
+ res = response ( context ) ;
23
+ } ) ;
24
+
25
+ it ( 'status' , ( ) => {
26
+ res . status ( 200 ) ;
27
+ expect ( res . statusCode ) . to . equal ( 200 ) ;
28
+ expect ( context . isDone ) . to . be . false ;
29
+ } ) ;
30
+
31
+ it ( 'sendStatus' , ( ) => {
32
+ res . sendStatus ( 204 ) ;
33
+ expect ( res . statusCode ) . to . equal ( 204 ) ;
34
+ expect ( context . isDone ) . to . be . true ;
35
+ } ) ;
36
+
37
+ it ( 'end' , ( ) => {
38
+ res . end ( 'test' ) ;
39
+ expect ( res . body ) . to . equal ( 'test' ) ;
40
+ expect ( context . isDone ) . to . be . true ;
41
+ } ) ;
42
+
43
+ it ( 'send' , ( ) => {
44
+ res . send ( 'test' ) ;
45
+ expect ( res . body ) . to . equal ( 'test' ) ;
46
+ expect ( context . isDone ) . to . be . true ;
47
+ } ) ;
48
+
49
+ it ( 'json' , ( ) => {
50
+ res . json ( 'test' ) ;
51
+ expect ( res . body ) . to . equal ( 'test' ) ;
52
+ expect ( res . get ( 'Content-Type' ) ) . to . equal ( 'application/json' ) ;
53
+ expect ( context . isDone ) . to . be . true ;
54
+ } ) ;
55
+
56
+ it ( 'set' , ( ) => {
57
+ res . set ( 'header' , 'val' ) ;
58
+ expect ( res . headers . header ) . to . equal ( 'val' ) ;
59
+ expect ( context . isDone ) . to . be . false ;
60
+ } ) ;
61
+
62
+ it ( 'header' , ( ) => {
63
+ res . header ( 'header' , 'val' ) ;
64
+ expect ( res . headers . header ) . to . equal ( 'val' ) ;
65
+ expect ( context . isDone ) . to . be . false ;
66
+ } ) ;
67
+
68
+ it ( 'type' , ( ) => {
69
+ res . type ( 'text/html' ) ;
70
+ expect ( res . get ( 'Content-Type' ) ) . to . equal ( 'text/html' ) ;
71
+ expect ( context . isDone ) . to . be . false ;
72
+ } ) ;
73
+
74
+ it ( 'get' , ( ) => {
75
+ res . set ( 'header' , 'val' ) ;
76
+ expect ( res . get ( 'header' ) ) . to . equal ( 'val' ) ;
77
+ expect ( context . isDone ) . to . be . false ;
78
+ } ) ;
79
+ } ) ;
80
+
81
+ describe ( 'request' , ( ) => {
82
+ var req , context ;
83
+
84
+ beforeEach ( ( ) => {
85
+ context = {
86
+ req : {
87
+ headers : { test : 'val' }
88
+ }
89
+ } ;
90
+
91
+ req = request ( context ) ;
92
+ } ) ;
93
+
94
+ it ( 'get' , ( ) => {
95
+ expect ( req . get ( 'test' ) ) . to . equal ( 'val' ) ;
96
+ } )
97
+ } ) ;
98
+ } ) ;
10
99
11
100
describe ( 'functions' , ( ) => {
101
+ var context = { } ;
102
+ var logs = [ ] ;
12
103
beforeEach ( ( ) => {
13
104
logs = [ ] ;
14
105
context = {
15
106
_inputs : [ ] ,
107
+ bindings : { } ,
16
108
log : ( message ) => logs . push ( message ) ,
17
109
bind : ( val , cb ) => cb && cb ( val )
18
110
} ;
@@ -144,5 +236,25 @@ describe('functions', () => {
144
236
done ( ) ;
145
237
} ) ;
146
238
} ) ;
239
+
240
+ it ( 'attaches context.res and context.req' , ( ) => {
241
+ var func = functions . createFunction ( ( context ) => {
242
+ context . res . status ( 200 )
243
+ . header ( 'header' , context . req . get ( 'field' ) )
244
+ . send ( 'test' ) ;
245
+ } ) ;
246
+
247
+ context . _triggerType = "httpTrigger" ;
248
+ context . req = {
249
+ headers : { 'field' : 'val' }
250
+ } ;
251
+ func ( context , ( results ) => {
252
+ expect ( context . res . statusCode ) . to . equal ( 200 ) ;
253
+ expect ( context . res . body ) . to . equal ( 'test' ) ;
254
+ expect ( context . res . headers . header ) . to . equal ( 'val' ) ;
255
+ expect ( context . _http ) . to . be . undefined ;
256
+ expect ( context . _done ) . to . be . true ;
257
+ } ) ;
258
+ } ) ;
147
259
} ) ;
148
260
} ) ;
0 commit comments