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.
Diff: tests/blocking/socket/HttpPostTest.cpp
- Revision:
- 0:836c9a6383e0
- Child:
- 1:5137ec8f4c45
diff -r 000000000000 -r 836c9a6383e0 tests/blocking/socket/HttpPostTest.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/blocking/socket/HttpPostTest.cpp Mon Aug 11 11:31:32 2014 +0000 @@ -0,0 +1,94 @@ +/* + * Copyright 2014, ACKme Networks + * All Rights Reserved. + * + * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks; + * the contents of this file may not be disclosed to third parties, copied + * or duplicated in any form, in whole or in part, without the prior + * written permission of ACKme Networks. + */ + + +#include "tests/Tests.h" +#include "Wiconnect.h" + + +#define TEST_SERVER_URL "http://www.posttestserver.com" +#define CONTEXT_TYPE "text/plain" + + +static WiconnectResult addPostData(Socket &socket); +static WiconnectResult readResponse(Socket &socket); + + +/*************************************************************************************************/ +WiconnectResult socketHttpPostCommand(int argc, char **argv) +{ + WiconnectResult result; + Wiconnect *wiconnect = Wiconnect::getInstance(); + Socket socket(0, NULL, sizeof(testBuffer), testBuffer); + uint32_t status; + + if(WICONNECT_FAILED(result, wiconnect->httpPost(socket, TEST_SERVER_URL, CONTEXT_TYPE))) + { + LOG_ERROR("Failed to open POST connection"); + } + else if(WICONNECT_FAILED(result, addPostData(socket))) + { + LOG_ERROR("Failed to add POST data"); + } + else if(WICONNECT_FAILED(result, wiconnect->httpGetStatus(socket, &status))) + { + LOG_ERROR("Failed to read HTTP status"); + } + else if(WICONNECT_FAILED(result, readResponse(socket))) + { + LOG_ERROR("Failed to read POST response"); + } + + return result; +} + +/*************************************************************************************************/ +static WiconnectResult addPostData(Socket &socket) +{ + WiconnectResult result; + + LOG_INFO("Enter post data ('\\n' issues request):"); + + int c; + + for(;;) + { + c = consoleSerial.getc(); + consoleSerial.putc(c); + if(c == '\r') + continue; + if(c == '\n') + break; + + if(WICONNECT_FAILED(result, socket.putc(c))) + { + return result; + } + } + + return socket.flushTxBuffer(); +} + +/*************************************************************************************************/ +static WiconnectResult readResponse(Socket &socket) +{ + uint8_t *buffer; + uint16_t size; + + LOG_INFO("Response data:"); + + while(socket.read(&buffer, &size) == WICONNECT_SUCCESS) + { + logWrite(buffer, size); + } + + return WICONNECT_SUCCESS; +} +