]> Git Repo - VerusCoin.git/blob - src/gtest/test_httprpc.cpp
Add test for issue #2444
[VerusCoin.git] / src / gtest / test_httprpc.cpp
1 #include <gtest/gtest.h>
2 #include <gmock/gmock.h>
3
4 #include "httprpc.cpp"
5 #include "httpserver.h"
6
7 using ::testing::Return;
8
9 class MockHTTPRequest : public HTTPRequest {
10 public:
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));
16
17     MockHTTPRequest() : HTTPRequest(nullptr) {}
18     void CleanUp() {
19         // So the parent destructor doesn't try to send a reply
20         replySent = true;
21     }
22 };
23
24 TEST(HTTPRPC, FailsOnGET) {
25     MockHTTPRequest req;
26     EXPECT_CALL(req, GetRequestMethod())
27         .WillRepeatedly(Return(HTTPRequest::GET));
28     EXPECT_CALL(req, WriteReply(HTTP_BAD_METHOD, "JSONRPC server handles only POST requests"))
29         .Times(1);
30     EXPECT_FALSE(HTTPReq_JSONRPC(&req, ""));
31     req.CleanUp();
32 }
33
34 TEST(HTTPRPC, FailsWithoutAuthHeader) {
35     MockHTTPRequest req;
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\""))
41         .Times(1);
42     EXPECT_CALL(req, WriteReply(HTTP_UNAUTHORIZED, ""))
43         .Times(1);
44     EXPECT_FALSE(HTTPReq_JSONRPC(&req, ""));
45     req.CleanUp();
46 }
47
48 TEST(HTTPRPC, FailsWithBadAuth) {
49     MockHTTPRequest req;
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\""))
57         .Times(1);
58     EXPECT_CALL(req, WriteReply(HTTP_UNAUTHORIZED, ""))
59         .Times(1);
60     EXPECT_FALSE(HTTPReq_JSONRPC(&req, ""));
61     req.CleanUp();
62 }
This page took 0.026368 seconds and 4 git commands to generate.