Fork of SandBox's original mbed-http (https://os.mbed.com/teams/sandbox/code/mbed-http/) and update for MbedOS6+ Content of TESTS folder was replaced with basic examples form original SandBox's HelloWorld
Revision 41:cc1a8c63f159, committed 2021-05-30
- Comitter:
- JohnnyK
- Date:
- Sun May 30 19:45:32 2021 +0000
- Parent:
- 40:6ecb52cd17d9
- Commit message:
- change test program to two basic example from SandBox's helloworld program ofc reworked.
Changed in this revision
diff -r 6ecb52cd17d9 -r cc1a8c63f159 TESTS/COMMON/test_setup.h --- a/TESTS/COMMON/test_setup.h Sat May 29 18:34:24 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -#ifndef _MBED_HTTP_TEST_SETUP_H_ -#define _MBED_HTTP_TEST_SETUP_H_ - -#include "mbed.h" -#include "NetworkInterface.h" - -/** - * Connect to the network using the default networking interface, - * you can also swap this out with a driver for a different networking interface - * if you use WiFi: see mbed_app.json for the credentials - */ -NetworkInterface *connect_to_default_network_interface() { - NetworkInterface* network = NetworkInterface::get_default_instance(); - - if (!network) { - return NULL; - } - - nsapi_error_t connect_status = network->connect(); - - if (connect_status != NSAPI_ERROR_OK) { - return NULL; - } - - return network; -} - - -/* List of trusted root CA certificates - * - * - Amazon, the CA for os.mbed.com - * - Let's Encrypt, the CA for httpbin.org - * - Comodo, the CA for reqres.in - * - * To add more root certificates, just concatenate them. - */ -const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" - "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" - "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" - "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n" - "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n" - "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n" - "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n" - "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n" - "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n" - "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n" - "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n" - "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n" - "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n" - "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n" - "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n" - "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n" - "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n" - "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" - "rqXRfboQnoZsG4q5WTP468SQvvG5\n" - "-----END CERTIFICATE-----\n" - "-----BEGIN CERTIFICATE-----\n" - "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" - "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzd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IBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n" - "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n" - "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n" - "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n" - "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n" - "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n" - "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n" - "-----END CERTIFICATE-----\n"; - -#endif // MBED_HTTP_TEST_SETUP_H_
diff -r 6ecb52cd17d9 -r cc1a8c63f159 TESTS/main-http.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TESTS/main-http.cpp Sun May 30 19:45:32 2021 +0000 @@ -0,0 +1,77 @@ +#if 0 /*Set it to "1" to enable content*/ + +#include "mbed.h" +#include "http_request.h" +#include "mbed_mem_trace.h" + +void dump_response(HttpResponse* res) { + printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +int main() { + printf("Example of a HTTP client\n"); + // Connect to the network with the default networking interface + // if you use WiFi: see mbed_app.json for the credentials + printf("[NWK] Connecting to network...\n"); + NetworkInterface* network = NetworkInterface::get_default_instance(); + if (!network) { + printf("[NWK] No network interface found, select an interface in 'network-helper.h'\n"); + return 1; + } + nsapi_error_t err; + if ((err = network->connect())!= NSAPI_ERROR_OK) { + printf("[NWK] Failed to connect to network (%d)\n", err); + return 1; + } + printf("[NWK] Connected to the network\n"); + SocketAddress ip; + network->get_ip_address(&ip); + printf("[NWK] IP address: %s\n", ip.get_ip_address()); + + + // Do a GET request to httpbin.org + { + // By default the body is automatically parsed and stored in a buffer, this is memory heavy. + // To receive chunked response, pass in a callback as last parameter to the constructor. + HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); + + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; + } + + printf("\n----- HTTP GET response -----\n"); + dump_response(get_res); + + delete get_req; + } + + // POST request to httpbin.org + { + HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); + post_req->set_header("Content-Type", "application/json"); + + const char body[] = "{\"hello\":\"world\"}"; + + HttpResponse* post_res = post_req->send(body, strlen(body)); + if (!post_res) { + printf("HttpRequest failed (error code %d)\n", post_req->get_error()); + return 1; + } + + printf("\n----- HTTP POST response -----\n"); + dump_response(post_res); + + delete post_req; + } + + while(osWaitForever); +} +#endif \ No newline at end of file
diff -r 6ecb52cd17d9 -r cc1a8c63f159 TESTS/main-https.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TESTS/main-https.cpp Sun May 30 19:45:32 2021 +0000 @@ -0,0 +1,140 @@ +#if 0 /*Set it to "1" to enable content*/ + +#include "mbed.h" +#include "mbed_trace.h" +#include "https_request.h" + +/* List of trusted root CA certificates + * currently two: Amazon, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org + * + * To add more root certificates, just concatenate them. + * + * !!! IF CA NOT WORKING SEE THIS PAGE - https://os.mbed.com/blog/entry/Adding-TLS-Sockets-to-Mbed-OS/ + * Under Windows you can run OpenSSl from C:\Program Files\Git\usr\bin\openssl.exe if you have Git installed. + * Then just executte "s_client -connect mbed.com:443 -showcerts" and "s_client -connect httpbin.org:443 -showcerts" + */ +const char SSL_CA_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8Hlf9BRUpJVeEXgSYCfOn8J3/yNTd126/+pZ\n" + "59vPr5KW7ySaNRB6nJHGDn2Z9j8Z3/VyVOEVqQdZe4O/Ui5GjLIAZHYcSNPYeehu\n" + "VsyuLAOQ1xk4meTKCRlb/weWsKh/NEnfVqn3sF/tM+2MR7cwA130A4w=\n" + "-----END CERTIFICATE-----\n"; + +void dump_response(HttpResponse* res) { + printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +int main() { + printf("Example of a HTTPS client\n"); + printf("[NWK] Connecting to network...\n"); + NetworkInterface* network = NetworkInterface::get_default_instance(); + if (!network) { + printf("[NWK] No network interface found, select an interface in 'network-helper.h'\n"); + return 1; + } + nsapi_error_t err; + if ((err = network->connect())!= NSAPI_ERROR_OK) { + printf("[NWK] Failed to connect to network (%d)\n", err); + return 1; + } + printf("[NWK] Connected to the network\n"); + SocketAddress ip; + network->get_ip_address(&ip); + printf("[NWK] IP address: %s\n", ip.get_ip_address()); + + mbed_trace_init(); + + // GET request to os.mbed.com + { + printf("\n----- HTTPS GET request -----\n"); + + HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt"); + + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; + } + printf("\n----- HTTPS GET response -----\n"); + dump_response(get_res); + + delete get_req; + } + + // POST request to httpbin.org + { + printf("\n----- HTTPS POST request -----\n"); + + HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); + post_req->set_header("Content-Type", "application/json"); + + const char body[] = "{\"hello\":\"world\"}"; + + HttpResponse* post_res = post_req->send(body, strlen(body)); + if (!post_res) { + printf("HttpRequest failed (error code %d)\n", post_req->get_error()); + return 1; + } + + printf("\n----- HTTPS POST response -----\n"); + dump_response(post_res); + + delete post_req; + } + + while(osWaitForever); +} + +#endif
diff -r 6ecb52cd17d9 -r cc1a8c63f159 TESTS/tests/integration/main.cpp --- a/TESTS/tests/integration/main.cpp Sat May 29 18:34:24 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,266 +0,0 @@ -/* - * PackageLicenseDeclared: Apache-2.0 - * Copyright (c) 2018 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "mbed.h" -#include "http_request.h" -#include "https_request.h" -#include "test_setup.h" -#include "utest/utest.h" -#include "unity/unity.h" -#include "greentea-client/test_env.h" - -using namespace utest::v1; - -static NetworkInterface *network; - -static void setup_verify_network() { - if (!network) network = connect_to_default_network_interface(); - TEST_ASSERT_NOT_NULL(network); -} - -// verifies that the header is present and has a certain value -static void assert_header(HttpResponse *res, const char *header, const char *value) { - bool headerPresent = false; - for (size_t ix = 0; ix < res->get_headers_length(); ix++) { - if (res->get_headers_fields()[ix]->compare(header)) { - headerPresent = true; - - TEST_ASSERT(res->get_headers_values()[ix]->compare(value)); - } - } - TEST_ASSERT_EQUAL(true, headerPresent); -} - -static control_t http_get(const size_t call_count) { - setup_verify_network(); - - HttpRequest *req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); - - HttpResponse* res = req->send(); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(418, res->get_status_code()); - - delete req; - - return CaseNext; -} - -static control_t http_post(const size_t call_count) { - setup_verify_network(); - - HttpRequest* req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); - req->set_header("Content-Type", "application/json"); - - const char body[] = "{\"mykey\":\"mbedvalue\"}"; - - HttpResponse* res = req->send(body, strlen(body)); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(200, res->get_status_code()); - - // verify that the Content-Type header is present, and set to application/json - assert_header(res, "Content-Type", "application/json"); - - // verify that both the key and value are present in the response - TEST_ASSERT(res->get_body_length() > 0); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("mykey"), string::npos); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("mbedvalue"), string::npos); - - delete req; - - return CaseNext; -} - -static control_t http_socket_reuse(const size_t call_count) { - setup_verify_network(); - - TCPSocket socket; - nsapi_error_t open_result = socket.open(network); - TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, open_result); - - nsapi_error_t connect_result = socket.connect("httpbin.org", 80); - TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, connect_result); - - { - HttpRequest *req = new HttpRequest(&socket, HTTP_GET, "http://httpbin.org/status/404"); - - HttpResponse* res = req->send(); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(404, res->get_status_code()); - - delete req; - } - - { - HttpRequest *req = new HttpRequest(&socket, HTTP_GET, "http://httpbin.org/status/403"); - - HttpResponse* res = req->send(); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(403, res->get_status_code()); - - delete req; - } - - return CaseNext; -} - -static control_t https_get(const size_t call_count) { - setup_verify_network(); - - HttpsRequest *req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt"); - - HttpResponse* res = req->send(); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(200, res->get_status_code()); - TEST_ASSERT(res->get_body_length() > 0); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("Hello world!"), string::npos); - - delete req; - - return CaseNext; -} - -static control_t https_post(const size_t call_count) { - setup_verify_network(); - - HttpsRequest* req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); - req->set_header("Content-Type", "application/json"); - - const char body[] = "{\"myhttpskey\":\"janjanjan\"}"; - - HttpResponse* res = req->send(body, strlen(body)); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(200, res->get_status_code()); - - // verify that the Content-Type header is present, and set to application/json - assert_header(res, "Content-Type", "application/json"); - - // verify that both the key and value are present in the response - TEST_ASSERT(res->get_body_length() > 0); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("myhttpskey"), string::npos); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("janjanjan"), string::npos); - - delete req; - - return CaseNext; -} - -static control_t https_socket_reuse(const size_t call_count) { - setup_verify_network(); - - TLSSocket socket; - nsapi_error_t open_result = socket.open(network); - TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, open_result); - - nsapi_error_t ca_result = socket.set_root_ca_cert(SSL_CA_PEM); - TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, ca_result); - - nsapi_error_t connect_result = socket.connect("httpbin.org", 443); - TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, connect_result); - - { - HttpsRequest *req = new HttpsRequest(&socket, HTTP_GET, "http://httpbin.org/status/404"); - - HttpResponse* res = req->send(); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(404, res->get_status_code()); - - delete req; - } - - { - HttpsRequest *req = new HttpsRequest(&socket, HTTP_GET, "http://httpbin.org/status/403"); - - HttpResponse* res = req->send(); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(403, res->get_status_code()); - - delete req; - } - - return CaseNext; -} - -// Spread the message out over 3 different chunks -const char * chunks[] = { - "{\"message\":", - "\"this is an example", - " of chunked encoding\"}" -}; - -int chunk_ix = 0; - -// Callback function, grab the next chunk and return it -const void * get_chunk(uint32_t* out_size) { - // If you don't have any data left, set out_size to 0 and return a null pointer - if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) { - *out_size = 0; - return NULL; - } - const char *chunk = chunks[chunk_ix]; - *out_size = strlen(chunk); - chunk_ix++; - - return chunk; -} - -static control_t chunked_request(const size_t call_count) { - setup_verify_network(); - - HttpsRequest *req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users"); - req->set_header("Content-Type", "application/json"); - - HttpResponse* res = req->send(&get_chunk); - TEST_ASSERT(res); - TEST_ASSERT_EQUAL(201, res->get_status_code()); - TEST_ASSERT(res->get_body_length() > 0); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("message"), string::npos); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("this is an example of chunked encoding"), string::npos); - TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("createdAt"), string::npos); - - delete req; - - return CaseNext; -} - -utest::v1::status_t greentea_setup(const size_t number_of_cases) { - GREENTEA_SETUP(1*60, "default_auto"); - return greentea_test_setup_handler(number_of_cases); -} - -Case cases[] = { - Case("http get", http_get), - Case("http post", http_post), - Case("http socket reuse", http_socket_reuse), - Case("https get", https_get), - Case("https post", https_post), - Case("https socket reuse", https_socket_reuse), - Case("chunked request", chunked_request) -}; - -Specification specification(greentea_setup, cases); - -void blink_led() { - static DigitalOut led(LED1); - led = !led; -} - -int main() { - Ticker t; - t.attach(blink_led, 0.5); - - return !Harness::run(specification); -}