| 32 | using namespace hueplusplus; |
| 33 | |
| 34 | TEST(HueCommandAPI, PUTRequest) |
| 35 | { |
| 36 | using namespace ::testing; |
| 37 | std::shared_ptr<MockHttpHandler> httpHandler = std::make_shared<MockHttpHandler>(); |
| 38 | |
| 39 | HueCommandAPI api(getBridgeIp(), getBridgePort(), getBridgeUsername(), httpHandler); |
| 40 | nlohmann::json request; |
| 41 | nlohmann::json result = nlohmann::json::object(); |
| 42 | result["ok"] = true; |
| 43 | |
| 44 | // empty path |
| 45 | { |
| 46 | EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername(), request, getBridgeIp(), 80)) |
| 47 | .WillOnce(Return(result)); |
| 48 | EXPECT_EQ(result, api.PUTRequest("", request)); |
| 49 | Mock::VerifyAndClearExpectations(httpHandler.get()); |
| 50 | } |
| 51 | // not empty path, starting with slash |
| 52 | { |
| 53 | const std::string path = "/test"; |
| 54 | EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)) |
| 55 | .WillOnce(Return(result)); |
| 56 | EXPECT_EQ(result, api.PUTRequest(path, request)); |
| 57 | Mock::VerifyAndClearExpectations(httpHandler.get()); |
| 58 | } |
| 59 | // not empty path, not starting with slash |
| 60 | { |
| 61 | const std::string path = "test"; |
| 62 | EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + '/' + path, request, getBridgeIp(), 80)) |
| 63 | .WillOnce(Return(result)); |
| 64 | EXPECT_EQ(result, api.PUTRequest(path, request)); |
| 65 | Mock::VerifyAndClearExpectations(httpHandler.get()); |
| 66 | } |
| 67 | // recoverable error |
| 68 | { |
| 69 | const std::string path = "/test"; |
| 70 | EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)) |
| 71 | .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) |
| 72 | .WillOnce(Return(result)); |
| 73 | EXPECT_EQ(result, api.PUTRequest(path, request)); |
| 74 | Mock::VerifyAndClearExpectations(httpHandler.get()); |
| 75 | } |
| 76 | // recoverable error x2 |
| 77 | { |
| 78 | const std::string path = "/test"; |
| 79 | EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)) |
| 80 | .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) |
| 81 | .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))); |
| 82 | EXPECT_THROW(api.PUTRequest(path, request), std::system_error); |
| 83 | Mock::VerifyAndClearExpectations(httpHandler.get()); |
| 84 | } |
| 85 | // unrecoverable error |
| 86 | { |
| 87 | const std::string path = "/test"; |
| 88 | EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)) |
| 89 | .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::not_enough_memory)))); |
| 90 | EXPECT_THROW(api.PUTRequest(path, request), std::system_error); |
| 91 | Mock::VerifyAndClearExpectations(httpHandler.get()); |
nothing calls this directly
no test coverage detected