|
| 1 | +#include <string> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#include <boost/test/unit_test.hpp> |
| 5 | + |
| 6 | +#include "tgbot/net/HttpClient.h" |
| 7 | +#include "tgbot/Api.h" |
| 8 | +#include "tgbot/TgException.h" |
| 9 | + |
| 10 | +using namespace std; |
| 11 | +using namespace TgBot; |
| 12 | + |
| 13 | +typedef TgException::ErrorCode TgErrorCode; |
| 14 | + |
| 15 | +class TestableApi : public Api { |
| 16 | +public: |
| 17 | + using Api::Api; |
| 18 | + using Api::sendRequest; |
| 19 | +}; |
| 20 | + |
| 21 | +class HttpClientMock : public HttpClient { |
| 22 | +public: |
| 23 | + std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const override |
| 24 | + {return response;}; |
| 25 | + |
| 26 | + int getRequestMaxRetries() const override { return 0;}; |
| 27 | + int getRequestBackoff() const override {return 1;}; |
| 28 | + |
| 29 | + string response; |
| 30 | +}; |
| 31 | + |
| 32 | +bool Request(TgErrorCode expectedCode, const string& response) { |
| 33 | + HttpClientMock httpClientMock; |
| 34 | + httpClientMock.response = response; |
| 35 | + |
| 36 | + TestableApi api("token", httpClientMock, "url"); |
| 37 | + |
| 38 | + try { |
| 39 | + api.sendRequest("", vector<HttpReqArg>()); |
| 40 | + } catch (TgException& exception) { |
| 41 | + return exception.errorCode == expectedCode; |
| 42 | + } |
| 43 | + |
| 44 | + return false; |
| 45 | +} |
| 46 | + |
| 47 | +BOOST_AUTO_TEST_SUITE(tApi) |
| 48 | + |
| 49 | +BOOST_AUTO_TEST_CASE(sendRequest) { |
| 50 | + BOOST_CHECK(Request(TgErrorCode::HtmlResponse, "<html>")); |
| 51 | + BOOST_CHECK(Request(TgErrorCode::Undefined, "{\"ok\": false}")); |
| 52 | + BOOST_CHECK(Request(TgErrorCode::Undefined, "{\"ok\": false, \"error_code\":0}")); |
| 53 | + |
| 54 | + BOOST_CHECK(Request(TgErrorCode::BadRequest, "{\"ok\": false, \"error_code\":400}")); |
| 55 | + BOOST_CHECK(Request(TgErrorCode::Unauthorized, "{\"ok\": false, \"error_code\":401}")); |
| 56 | + BOOST_CHECK(Request(TgErrorCode::Forbidden, "{\"ok\": false, \"error_code\":403}")); |
| 57 | + BOOST_CHECK(Request(TgErrorCode::NotFound, "{\"ok\": false, \"error_code\":404}")); |
| 58 | + BOOST_CHECK(Request(TgErrorCode::Flood, "{\"ok\": false, \"error_code\":402}")); |
| 59 | + BOOST_CHECK(Request(TgErrorCode::Internal, "{\"ok\": false, \"error_code\":500}")); |
| 60 | + |
| 61 | + BOOST_CHECK(Request(TgErrorCode::InvalidJson, "error_code:101")); |
| 62 | +} |
| 63 | + |
| 64 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments