For bug fixes
Fork of MTS-Socket by
Diff: Test/TestHTTP.h
- Revision:
- 7:08b474178245
- Child:
- 8:a3b41ec82e63
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/TestHTTP.h Tue Jun 03 14:49:26 2014 +0000 @@ -0,0 +1,123 @@ +#ifndef TESTHTTP_H +#define TESTHTTP_H + +#include "mtsas.h" +#include <string> + +using namespace mts; + +class TestHTTP : public TestCollection +{ +public: + TestHTTP(); + virtual void run(); + +private: + MTSSerialFlowControl* io; + Cellular* radio; + HTTPClient* http; + char sbuf[1024]; + char rbuf[1024]; + HTTPText* send; + HTTPText* receive; +}; + +TestHTTP::TestHTTP() : TestCollection("TestHTTP") {} + +void TestHTTP::run() { + string data; + string url; + string base_url = "http://httpbin.org:80"; + string url_get = "/get"; + string url_put = "/put"; + string url_post = "/post"; + string url_del = "/delete"; + + MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); + + Test::start("Setup"); + io = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); + io->baud(115200); + radio = CellularFactory::create(io); + if (! radio) { + Test::assertTrue(false); + } + radio->configureSignals(PTA4, PTC9, PTA20); + Transport::setTransport(radio); + + http = new HTTPClient(); + send = new HTTPText(sbuf); + receive = new HTTPText(rbuf); + + for (int i = 0; i < 10; i++) { + if (i >= 10) { + Test::assertTrue(false); + } + if (radio->setApn("wap.cingular") == SUCCESS) { + break; + } else { + wait(1); + } + } + for (int i = 0; i < 3; i++) { + if (i >= 3) { + Test::assertTrue(false); + } + if (radio->connect()) { + break; + } else { + wait(1); + } + } + + for (int i = 0; i < 5; i++) { + if (i >= 5) { + Test::assertTrue(false); + } + if (radio->ping()) { + break; + } else { + wait(1); + } + } + Test::end(); + + url = base_url + url_get; + Test::start("HTTP GET"); + logInfo("testing GET"); + Test::assertTrue(http->get(url.c_str(), receive, 5000) == HTTP_OK); + logInfo("done testing GET"); + Test::end(); + + url = base_url + url_put; + data = "testing HTTP PUT method"; + Test::start("HTTP PUT"); + logInfo("testing PUT"); + strncpy(sbuf, data.c_str(), sizeof(sbuf)); + Test::assertTrue(http->put(url.c_str(), *send, receive, 5000) == HTTP_OK); + Test::assertTrue(string(rbuf).find(data) != string::npos); + logInfo("done testing PUT"); + Test::end(); + + url = base_url + url_post; + data = "testing HTTP POST method"; + Test::start("HTTP POST"); + logInfo("testing POST"); + strncpy(sbuf, data.c_str(), sizeof(sbuf)); + Test::assertTrue(http->post(url.c_str(), *send, receive, 5000) == HTTP_OK); + Test::assertTrue(string(rbuf).find(data) != string::npos); + logInfo("done testing POST"); + Test::end(); + + url = base_url + url_del; + data = "testing HTTP DELETE method"; + Test::start("HTTP DELETE"); + logInfo("testing DELETE"); + Test::assertTrue(http->del(url.c_str(), receive, 5000) == HTTP_OK); + logInfo("done testing DELETE"); + Test::end(); + + radio->disconnect(); +} + +#endif \ No newline at end of file