Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MTS-Socket by
TestHTTPS.h
00001 #ifndef TESTHTTPS_H 00002 #define TESTHTTPS_H 00003 00004 #include "mtsas.h" 00005 #include <string> 00006 #include "httpbinCert.h" 00007 00008 using namespace mts; 00009 00010 class TestHTTPS : public TestCollection 00011 { 00012 public: 00013 TestHTTPS(); 00014 virtual void run(); 00015 00016 private: 00017 MTSSerialFlowControl* io; 00018 Cellular* radio; 00019 HTTPClient* http; 00020 char rbuf[1024]; 00021 HTTPMap* send; 00022 HTTPText* receive; 00023 }; 00024 00025 TestHTTPS::TestHTTPS() : TestCollection("TestHTTPS") {} 00026 00027 void TestHTTPS::run() { 00028 00029 00030 string url; 00031 string secure_url = "https://httpbin.org:443"; 00032 string url_get = "/get"; 00033 string url_put = "/put"; 00034 string url_post = "/post"; 00035 string url_del = "/delete"; 00036 string url_stream = "/stream/20"; 00037 string url_auth = "/basic-auth"; 00038 00039 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); 00040 00041 Test::start("Setup"); 00042 io = new MTSSerialFlowControl(D8, D2, D3, D6); 00043 io->baud(115200); 00044 radio = CellularFactory::create(io); 00045 if (! radio) { 00046 Test::assertTrue(false); 00047 } 00048 radio->configureSignals(D4, D7, RESET); 00049 Transport::setTransport(radio); 00050 00051 http = new HTTPClient(); 00052 send = new HTTPMap(); 00053 receive = new HTTPText(rbuf); 00054 00055 for (int i = 0; i < 10; i++) { 00056 if (i >= 10) { 00057 Test::assertTrue(false); 00058 } 00059 if (radio->setApn(APN) == MTS_SUCCESS) { 00060 break; 00061 } else { 00062 wait(1); 00063 } 00064 } 00065 for (int i = 0; i < 3; i++) { 00066 if (i >= 3) { 00067 Test::assertTrue(false); 00068 } 00069 if (radio->connect()) { 00070 break; 00071 } else { 00072 wait(1); 00073 } 00074 } 00075 00076 for (int i = 0; i < 5; i++) { 00077 if (i >= 5) { 00078 Test::assertTrue(false); 00079 } 00080 if (radio->ping()) { 00081 break; 00082 } else { 00083 wait(1); 00084 } 00085 } 00086 00087 if (http->addRootCACertificate(CERTIFICATE) != HTTP_OK) { 00088 Test::assertTrue(false); 00089 } 00090 http->setPeerVerification(VERIFY_PEER); 00091 00092 Test::end(); 00093 00094 url = secure_url + url_get; 00095 Test::start("HTTPS GET"); 00096 Test::assertTrue(http->get(url.c_str(), receive, 10000) == HTTP_OK); 00097 Test::end(); 00098 00099 url = secure_url + url_put; 00100 Test::start("HTTPS PUT"); 00101 send->put("testing", "put"); 00102 Test::assertTrue(http->put(url.c_str(), *send, receive, 10000) == HTTP_OK); 00103 send->clear(); 00104 Test::end(); 00105 00106 url = secure_url + url_post; 00107 Test::start("HTTPS POST"); 00108 send->put("testing", "put"); 00109 Test::assertTrue(http->post(url.c_str(), *send, receive, 10000) == HTTP_OK); 00110 send->clear(); 00111 Test::end(); 00112 00113 url = secure_url + url_del; 00114 Test::start("HTTPS DELETE"); 00115 Test::assertTrue(http->del(url.c_str(), receive, 10000) == HTTP_OK); 00116 Test::end(); 00117 00118 url = secure_url + url_stream; 00119 char bigbuf[8 * 1024]; 00120 HTTPText big(bigbuf); 00121 Test::start("HTTPS big GET"); 00122 Test::assertTrue(http->get(url.c_str(), &big, 10000) == HTTP_OK); 00123 Test::end(); 00124 00125 string url_usr_pw = "/Bob/123"; 00126 url = secure_url + url_auth + url_usr_pw; 00127 const char user[] = "Bob",pw[] = "123"; 00128 Test::start("HTTPS AUTH GET"); 00129 Test::assertTrue(http->basicAuth(user,pw) == HTTP_OK); 00130 Test::assertTrue(http->get(url.c_str(), receive, 30000) == HTTP_OK); 00131 Test::assertTrue(http->basicAuth(NULL,NULL) == HTTP_OK); 00132 Test::end(); 00133 00134 radio->disconnect(); 00135 } 00136 00137 #endif
Generated on Wed Jul 13 2022 10:29:47 by
