@@ -6,68 +6,79 @@ jest.mock("@azure/arm-appservice")
6
6
jest . mock ( "@azure/arm-resources" )
7
7
jest . mock ( "./functionAppService" )
8
8
import { FunctionAppService } from "./functionAppService" ;
9
+ import configConstants from "../config" ;
9
10
10
11
describe ( "Invoke Service " , ( ) => {
11
12
const app = MockFactory . createTestSite ( ) ;
12
13
const expectedSite = MockFactory . createTestSite ( ) ;
13
14
const testData = "test-data" ;
14
- const testResult = "test-data " ;
15
+ const testResult = "test result " ;
15
16
const authKey = "authKey" ;
16
17
const baseUrl = "https://management.azure.com"
17
18
const masterKeyUrl = `https://${ app . defaultHostName } /admin/host/systemkeys/_master` ;
18
19
const authKeyUrl = `${ baseUrl } ${ app . id } /functions/admin/token?api-version=2016-08-01` ;
19
- let urlPOST = `http://${ app . defaultHostName } /api/hello` ;
20
- let urlGET = `http://${ app . defaultHostName } /api/hello?name%3D${ testData } ` ;
20
+ const functionName = "hello" ;
21
+ const urlPOST = `http://${ app . defaultHostName } /api/${ functionName } ` ;
22
+ const urlGET = `http://${ app . defaultHostName } /api/${ functionName } ?name%3D${ testData } ` ;
23
+ const localUrl = `http://localhost:${ configConstants . defaultLocalPort } /api/${ functionName } `
21
24
let masterKey : string ;
25
+ let sls = MockFactory . createTestServerless ( ) ;
26
+ let options = {
27
+ function : functionName ,
28
+ data : JSON . stringify ( { name : testData } ) ,
29
+ method : "GET"
30
+ } as any ;
22
31
23
32
beforeAll ( ( ) => {
24
33
const axiosMock = new MockAdapter ( axios ) ;
25
34
// Master Key
26
35
axiosMock . onGet ( masterKeyUrl ) . reply ( 200 , { value : masterKey } ) ;
27
36
// Auth Key
28
37
axiosMock . onGet ( authKeyUrl ) . reply ( 200 , authKey ) ;
29
- //Mock url for GET
38
+ // Mock url for GET
30
39
axiosMock . onGet ( urlGET ) . reply ( 200 , testResult ) ;
31
- //Mock url for POST
40
+ // Mock url for POST
32
41
axiosMock . onPost ( urlPOST ) . reply ( 200 , testResult ) ;
42
+ // Mock url for local POST
43
+ axiosMock . onPost ( localUrl ) . reply ( 200 , testResult ) ;
33
44
} ) ;
34
-
45
+
35
46
beforeEach ( ( ) => {
36
47
FunctionAppService . prototype . getMasterKey = jest . fn ( ) ;
37
48
FunctionAppService . prototype . get = jest . fn ( ( ) => Promise . resolve ( expectedSite ) ) ;
49
+ FunctionAppService . prototype . getFunctionHttpTriggerConfig = jest . fn ( ( ) => {
50
+ return { url : `${ app . defaultHostName } /api/hello` }
51
+ } ) as any ;
52
+ sls = MockFactory . createTestServerless ( ) ;
53
+ options = {
54
+ function : functionName ,
55
+ data : JSON . stringify ( { name : testData } ) ,
56
+ method : "GET"
57
+ } as any ;
38
58
} ) ;
39
59
40
60
it ( "Invokes a function with GET request" , async ( ) => {
41
- const sls = MockFactory . createTestServerless ( ) ;
42
- const options = MockFactory . createTestServerlessOptions ( ) ;
43
- const expectedResult = { url : `${ app . defaultHostName } /api/hello` } ;
44
- const httpConfig = jest . fn ( ( ) => expectedResult ) ;
45
-
46
- FunctionAppService . prototype . getFunctionHttpTriggerConfig = httpConfig as any ;
47
-
48
- options [ "function" ] = "hello" ;
49
- options [ "data" ] = `{"name": "${ testData } "}` ;
50
- options [ "method" ] = "GET" ;
51
-
52
61
const service = new InvokeService ( sls , options ) ;
53
- const response = await service . invoke ( options [ " method" ] , options [ " function" ] , options [ " data" ] ) ;
62
+ const response = await service . invoke ( options . method , options . function , options . data ) ;
54
63
expect ( JSON . stringify ( response . data ) ) . toEqual ( JSON . stringify ( testResult ) ) ;
55
64
} ) ;
56
65
57
66
it ( "Invokes a function with POST request" , async ( ) => {
58
- const sls = MockFactory . createTestServerless ( ) ;
59
- const options = MockFactory . createTestServerlessOptions ( ) ;
60
- const expectedResult = { url : `${ app . defaultHostName } /api/hello` } ;
61
- const httpConfig = jest . fn ( ( ) => expectedResult ) ;
62
- FunctionAppService . prototype . getFunctionHttpTriggerConfig = httpConfig as any ;
63
-
64
- options [ "function" ] = "hello" ;
65
- options [ "data" ] = `{"name": "${ testData } "}` ;
66
- options [ "method" ] = "POST" ;
67
-
68
67
const service = new InvokeService ( sls , options ) ;
69
- const response = await service . invoke ( options [ "method" ] , options [ "function" ] , options [ "data" ] ) ;
68
+ const response = await service . invoke ( options . method , options . function , options . data ) ;
69
+ expect ( JSON . stringify ( response . data ) ) . toEqual ( JSON . stringify ( testResult ) ) ;
70
+ expect ( FunctionAppService . prototype . getFunctionHttpTriggerConfig ) . toBeCalled ( ) ;
71
+ expect ( FunctionAppService . prototype . get ) . toBeCalled ( ) ;
72
+ expect ( FunctionAppService . prototype . getMasterKey ) . toBeCalled ( ) ;
73
+ } ) ;
74
+
75
+ it ( "Invokes a local function" , async ( ) => {
76
+ options . method = "POST" ;
77
+ const service = new InvokeService ( sls , options , true ) ;
78
+ const response = await service . invoke ( options . method , options . function , options . data ) ;
70
79
expect ( JSON . stringify ( response . data ) ) . toEqual ( JSON . stringify ( testResult ) ) ;
80
+ expect ( FunctionAppService . prototype . getFunctionHttpTriggerConfig ) . not . toBeCalled ( ) ;
81
+ expect ( FunctionAppService . prototype . get ) . not . toBeCalled ( ) ;
82
+ expect ( FunctionAppService . prototype . getMasterKey ) . not . toBeCalled ( ) ;
71
83
} ) ;
72
-
73
- } ) ;
84
+ } ) ;
0 commit comments