Page 48 - MSDN Magazine, December 15, 2017
P. 48

Figure 2 Configuring a Mock API in Azure Functions Proxies
Use Postman, or your preferred API testing tool, to send a GET to the proxy URL. You should receive back your formatted JSON object in the response body, like so:
and receive that username back. Create a new proxy, but this time click on the Advanced editor. This will open up the Monaco editor, and allow you to directly edit the proxies.json file. Proxies. json sits at the root directory of your function app, and controls the proxy rules shown in the UI. Paste in the code from Figure 3 above the mobile-user proxy, not- ing proper commas and brackets
in the JSON object.
Note that you can use
request.headers.headername to insert request header values into a response. This is perfect for mak- ing more interactive mock APIs.
This proxy will create a new mock endpoint at /mobile-user that responds to POST requests. The new endpoint will return a response body with the user that the client posted as a request header. Send a POST request to the /mobile-user endpoint using Post- man, and add a User header with your own username. The response
back should contain that username in the response body, like so:
{ {}
"Name": "Proxies Connect"
You can add as many request overrides as you want, to create more complex hardcoded mock APIs with different headers, status codes or body contents.
Creating an Interactive Mock API The mobile devs have tested a basic connection with your static mock API, but now they want something with dynamic content. We’ll create a new mock API that allows them to test out a POST operation. They’ll POST a username
Figure 3 An interactive Mock API
}
}
"headers": {
"User": "MyUsername"
Utilizing the request parameter, you can insert all sorts of request info into the response: Request.method, request.headers.<headername>, and request.querystring.<para- matername> are all available to you in the response overrides.
Moving forward implementing the API, it’s important to note that request and response overrides are additive, overwriting any
Figure 4 The API Response to the Proxied /Mobile-User Endpoint
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip,deflate",
"Cache-Control": "no-cache",
"Connection": "close",
"Disguised-Host": "connectfndemo.azurewebsites.net", "Host": "httpbin.org",
"Max-Forwards": "9",
"Postman-Token": "5541efc6-709a-4e95-b782-cbc946452564", "User": "MyUsername",
"User-Agent": "PostmanRuntime/6.4.1", "Was-Default-Hostname": "connectfndemo.azurewebsites.net", "X-Arr-Log-Id": "60cf1436-ca3d-4408-801e-8cf47afd4a46", "X-Arr-Ssl": "2048|256|C=US, |CN=*.azurewebsites.net", "X-Ms-Ext-Routing": "1",
"X-Original-Url": "/mobile-user",
"X-Site-Deployment-Id": "ConnectFnDemo", "X-Waws-Unencoded-Url": "/mobile-user"
} }
"MobileUserPOST": { "matchCondition": {
"route": "/mobile-user", "methods": [
"POST" ]
}, "responseOverrides": {
"response.statusCode": "200", "response.statusReason": "OK", "response.body":
{ "headers":{
"User":"request.headers.User" }
},
"response.headers.Content-Type": "Application/JSON"
} },
44 msdn magazine
Functions






















































   46   47   48   49   50