1 #include <gtest/gtest.h>
2 #include <gmock/gmock.h>
5 #include "httpserver.h"
7 using ::testing::Return;
9 class MockHTTPRequest : public HTTPRequest {
11 MOCK_METHOD0(GetPeer, CService());
12 MOCK_METHOD0(GetRequestMethod, HTTPRequest::RequestMethod());
13 MOCK_METHOD1(GetHeader, std::pair<bool, std::string>(const std::string& hdr));
14 MOCK_METHOD2(WriteHeader, void(const std::string& hdr, const std::string& value));
15 MOCK_METHOD2(WriteReply, void(int nStatus, const std::string& strReply));
17 MockHTTPRequest() : HTTPRequest(nullptr) {}
19 // So the parent destructor doesn't try to send a reply
24 TEST(HTTPRPC, FailsOnGET) {
26 EXPECT_CALL(req, GetRequestMethod())
27 .WillRepeatedly(Return(HTTPRequest::GET));
28 EXPECT_CALL(req, WriteReply(HTTP_BAD_METHOD, "JSONRPC server handles only POST requests"))
30 EXPECT_FALSE(HTTPReq_JSONRPC(&req, ""));
34 TEST(HTTPRPC, FailsWithoutAuthHeader) {
36 EXPECT_CALL(req, GetRequestMethod())
37 .WillRepeatedly(Return(HTTPRequest::POST));
38 EXPECT_CALL(req, GetHeader("authorization"))
39 .WillRepeatedly(Return(std::make_pair(false, "")));
40 EXPECT_CALL(req, WriteHeader("WWW-Authenticate", "Basic realm=\"jsonrpc\""))
42 EXPECT_CALL(req, WriteReply(HTTP_UNAUTHORIZED, ""))
44 EXPECT_FALSE(HTTPReq_JSONRPC(&req, ""));
48 TEST(HTTPRPC, FailsWithBadAuth) {
50 EXPECT_CALL(req, GetRequestMethod())
51 .WillRepeatedly(Return(HTTPRequest::POST));
52 EXPECT_CALL(req, GetHeader("authorization"))
53 .WillRepeatedly(Return(std::make_pair(true, "Basic spam:eggs")));
54 EXPECT_CALL(req, GetPeer())
55 .WillRepeatedly(Return(CService("127.0.0.1:1337")));
56 EXPECT_CALL(req, WriteHeader("WWW-Authenticate", "Basic realm=\"jsonrpc\""))
58 EXPECT_CALL(req, WriteReply(HTTP_UNAUTHORIZED, ""))
60 EXPECT_FALSE(HTTPReq_JSONRPC(&req, ""));