15
15
'use strict' ;
16
16
17
17
const sinon = require ( 'sinon' ) ;
18
- const proxyquire = require ( 'proxyquire' ) . noCallThru ( ) ;
19
18
const assert = require ( 'assert' ) ;
20
-
21
- const getSample = ( ) => {
22
- const requestPromise = sinon
23
- . stub ( )
24
- . returns ( new Promise ( resolve => resolve ( 'test' ) ) ) ;
25
-
26
- return {
27
- sample : proxyquire ( '../' , {
28
- 'request-promise' : requestPromise ,
29
- } ) ,
30
- mocks : {
31
- requestPromise : requestPromise ,
32
- } ,
33
- } ;
34
- } ;
19
+ const { getFunction} = require ( '@google-cloud/functions-framework/testing' ) ;
35
20
36
21
const getMocks = ( ) => {
37
22
const req = {
@@ -76,12 +61,14 @@ beforeEach(stubConsole);
76
61
afterEach ( restoreConsole ) ;
77
62
78
63
describe ( 'functions_http_content' , ( ) => {
64
+ require ( '..' ) ;
65
+ const helloContent = getFunction ( 'helloContent' ) ;
66
+
79
67
it ( 'http:helloContent: should handle application/json' , ( ) => {
80
68
const mocks = getMocks ( ) ;
81
- const httpSample = getSample ( ) ;
82
69
mocks . req . headers [ 'content-type' ] = 'application/json' ;
83
70
mocks . req . body = { name : 'John' } ;
84
- httpSample . sample . helloContent ( mocks . req , mocks . res ) ;
71
+ helloContent ( mocks . req , mocks . res ) ;
85
72
86
73
assert . strictEqual ( mocks . res . status . calledOnce , true ) ;
87
74
assert . strictEqual ( mocks . res . status . firstCall . args [ 0 ] , 200 ) ;
@@ -91,10 +78,9 @@ describe('functions_http_content', () => {
91
78
92
79
it ( 'http:helloContent: should handle application/octet-stream' , ( ) => {
93
80
const mocks = getMocks ( ) ;
94
- const httpSample = getSample ( ) ;
95
81
mocks . req . headers [ 'content-type' ] = 'application/octet-stream' ;
96
82
mocks . req . body = Buffer . from ( 'John' ) ;
97
- httpSample . sample . helloContent ( mocks . req , mocks . res ) ;
83
+ helloContent ( mocks . req , mocks . res ) ;
98
84
99
85
assert . strictEqual ( mocks . res . status . calledOnce , true ) ;
100
86
assert . strictEqual ( mocks . res . status . firstCall . args [ 0 ] , 200 ) ;
@@ -104,10 +90,9 @@ describe('functions_http_content', () => {
104
90
105
91
it ( 'http:helloContent: should handle text/plain' , ( ) => {
106
92
const mocks = getMocks ( ) ;
107
- const httpSample = getSample ( ) ;
108
93
mocks . req . headers [ 'content-type' ] = 'text/plain' ;
109
94
mocks . req . body = 'John' ;
110
- httpSample . sample . helloContent ( mocks . req , mocks . res ) ;
95
+ helloContent ( mocks . req , mocks . res ) ;
111
96
112
97
assert . strictEqual ( mocks . res . status . calledOnce , true ) ;
113
98
assert . strictEqual ( mocks . res . status . firstCall . args [ 0 ] , 200 ) ;
@@ -117,10 +102,9 @@ describe('functions_http_content', () => {
117
102
118
103
it ( 'http:helloContent: should handle application/x-www-form-urlencoded' , ( ) => {
119
104
const mocks = getMocks ( ) ;
120
- const httpSample = getSample ( ) ;
121
105
mocks . req . headers [ 'content-type' ] = 'application/x-www-form-urlencoded' ;
122
106
mocks . req . body = { name : 'John' } ;
123
- httpSample . sample . helloContent ( mocks . req , mocks . res ) ;
107
+ helloContent ( mocks . req , mocks . res ) ;
124
108
125
109
assert . strictEqual ( mocks . res . status . calledOnce , true ) ;
126
110
assert . strictEqual ( mocks . res . status . firstCall . args [ 0 ] , 200 ) ;
@@ -130,8 +114,7 @@ describe('functions_http_content', () => {
130
114
131
115
it ( 'http:helloContent: should handle other' , ( ) => {
132
116
const mocks = getMocks ( ) ;
133
- const httpSample = getSample ( ) ;
134
- httpSample . sample . helloContent ( mocks . req , mocks . res ) ;
117
+ helloContent ( mocks . req , mocks . res ) ;
135
118
136
119
assert . strictEqual ( mocks . res . status . calledOnce , true ) ;
137
120
assert . strictEqual ( mocks . res . status . firstCall . args [ 0 ] , 200 ) ;
@@ -141,10 +124,9 @@ describe('functions_http_content', () => {
141
124
142
125
it ( 'http:helloContent: should escape XSS' , ( ) => {
143
126
const mocks = getMocks ( ) ;
144
- const httpSample = getSample ( ) ;
145
127
mocks . req . headers [ 'content-type' ] = 'text/plain' ;
146
128
mocks . req . body = { name : '<script>alert(1)</script>' } ;
147
- httpSample . sample . helloContent ( mocks . req , mocks . res ) ;
129
+ helloContent ( mocks . req , mocks . res ) ;
148
130
149
131
assert . strictEqual ( mocks . res . status . calledOnce , true ) ;
150
132
assert . strictEqual ( mocks . res . status . firstCall . args [ 0 ] , 200 ) ;
0 commit comments