|
| 1 | +# |
| 2 | +# Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import requests, json |
| 18 | +from bdd_test_util import bdd_log |
| 19 | + |
| 20 | +CORE_REST_PORT = "7050" |
| 21 | +ACCEPT_JSON_HEADER = { |
| 22 | + 'Accept': 'application/json' |
| 23 | +} |
| 24 | + |
| 25 | +def httpGetToContainerAlias(context, containerAlias, endpoint, \ |
| 26 | + port=CORE_REST_PORT, expectSuccess=True): |
| 27 | + """ Performs a GET to the given container doing a lookup by alias first. |
| 28 | + Optional args are port (defaults to the default peer rest port) and the |
| 29 | + expectSuccess which validates the response returned a 200 """ |
| 30 | + container = context.containerAliasMap[containerAlias] |
| 31 | + return httpGetToContainer(context, container, endpoint, port, expectSuccess) |
| 32 | + |
| 33 | +def httpGetToContainer(context, container, endpoint, \ |
| 34 | + port=CORE_REST_PORT, expectSuccess=True): |
| 35 | + """ Performs a GET to the given container. Optional args are port (defaults |
| 36 | + to the default peer rest port) and the expectSuccess which validates the |
| 37 | + response returned a 200 """ |
| 38 | + request_url = buildContainerUrl(context, container, endpoint, port) |
| 39 | + return httpGet(request_url, expectSuccess) |
| 40 | + |
| 41 | +def httpPostToContainerAlias(context, containerAlias, endpoint, body, \ |
| 42 | + port=CORE_REST_PORT, expectSuccess=True): |
| 43 | + """ Performs a POST to the given container doing a lookup by alias first. |
| 44 | + Optional args are port (defaults to the default peer rest port) and the |
| 45 | + expectSuccess which validates the response returned a 200 """ |
| 46 | + container = context.containerAliasMap[containerAlias] |
| 47 | + return httpPostToContainer(context, container, endpoint, body, port, expectSuccess) |
| 48 | + |
| 49 | +def httpPostToContainer(context, container, endpoint, body, \ |
| 50 | + port=CORE_REST_PORT, expectSuccess=True): |
| 51 | + """ Performs a GET to the given container. Optional args are port (defaults |
| 52 | + to the default peer rest port) and the expectSuccess which validates the |
| 53 | + response returned a 200 """ |
| 54 | + request_url = buildContainerUrl(context, container, endpoint, port) |
| 55 | + return httpPost(request_url, body, expectSuccess) |
| 56 | + |
| 57 | +def buildContainerAliasUrl(context, containerAlias, endpoint, port=CORE_REST_PORT): |
| 58 | + """ Build a URL to do a HTTP request to the given container looking up the |
| 59 | + alias first. Optionally provide a port too which defaults to the peer |
| 60 | + rest port """ |
| 61 | + container = context.containerAliasMap[containerAlias] |
| 62 | + return buildContainerUrl(context, container, endpoint, port=port) |
| 63 | + |
| 64 | +def buildContainerUrl(context, container, endpoint, port=CORE_REST_PORT): |
| 65 | + """ Build a URL to do a HTTP request to the given container. Optionally |
| 66 | + provide a port too which defaults to the peer rest port """ |
| 67 | + return buildUrl(context, container.ipAddress, port, endpoint) |
| 68 | + |
| 69 | +def buildUrl(context, ipAddress, port, endpoint): |
| 70 | + schema = "http" |
| 71 | + if 'TLS' in context.tags: |
| 72 | + schema = "https" |
| 73 | + return "{0}://{1}:{2}{3}".format(schema, ipAddress, port, endpoint) |
| 74 | + |
| 75 | +def httpGet(url, expectSuccess=True): |
| 76 | + return _request("GET", url, expectSuccess=expectSuccess) |
| 77 | + |
| 78 | +def httpPost(url, body, expectSuccess=True): |
| 79 | + return _request("POST", url, json=body, expectSuccess=expectSuccess) |
| 80 | + |
| 81 | +def _request(method, url, expectSuccess=True, **kwargs): |
| 82 | + bdd_log("HTTP {} to url = {}".format(method, url)) |
| 83 | + |
| 84 | + response = requests.request(method, url, \ |
| 85 | + headers=ACCEPT_JSON_HEADER, verify=False, **kwargs) |
| 86 | + |
| 87 | + if expectSuccess: |
| 88 | + assert response.status_code == 200, \ |
| 89 | + "Failed to {} to {}: {}".format(method, url, response.text) |
| 90 | + |
| 91 | + bdd_log("Response from {}:".format(url)) |
| 92 | + bdd_log(formatResponseText(response)) |
| 93 | + |
| 94 | + return response |
| 95 | + |
| 96 | +def formatResponseText(response): |
| 97 | + # Limit to 300 chars because of the size of the Base64 encoded Certs |
| 98 | + return json.dumps(response.json(), indent = 4)[:300] |
0 commit comments