httpmockserver
test_environment.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include <gtest/gtest.h>
10 #include <type_traits>
11 #include "mock_server.h"
12 #include "mock_holder.h"
13 #include "port_searcher.h"
14 
15 
16 namespace httpmock {
17 
18 
19 template <class HTTPMock>
20 class TestEnvironment: public ::testing::Environment {
21  HTTPMock httpServer;
22  public:
24  : httpServer()
25  {
26  static_assert(std::is_base_of<MockServer, HTTPMock>::value,
27  "An instance derived from httpmock::MockServer required!");
28  }
29 
30  TestEnvironment(HTTPMock &&mock)
31  : httpServer(std::move(mock))
32  {
33  // We want an interface of the IMockServer to be fulfilled.
34  static_assert(std::is_base_of<IMockServer, HTTPMock>::value,
35  "An instance derived from httpmock::IMockServer required!");
36  }
37 
38  virtual ~TestEnvironment() {}
39 
40  virtual void SetUp() {
41  if (not httpServer.isRunning()) {
42  httpServer.start();
43  }
44  }
45 
46  virtual void TearDown() {
47  httpServer.stop();
48  }
49 
50  const HTTPMock &getMock() const {
51  return httpServer;
52  }
53 };
54 
55 
61 template <typename HTTPMock>
62 ::testing::Environment *createMockServerEnvironment(
63  unsigned startPort = 8080, unsigned tryCount = 1000)
64 {
66  getFirstRunningMockServer<HTTPMock>(startPort, tryCount));
67 }
68 
69 
70 
71 } // namespace httpmock
mock_server.h
httpmock::TestEnvironment
Definition: test_environment.h:20
httpmock::TestEnvironment::~TestEnvironment
virtual ~TestEnvironment()
Definition: test_environment.h:38
httpmock
Definition: mock_server.cc:20
httpmock::TestEnvironment::httpServer
HTTPMock httpServer
Definition: test_environment.h:21
httpmock::createMockServerEnvironment
::testing::Environment * createMockServerEnvironment(unsigned startPort=8080, unsigned tryCount=1000)
Definition: test_environment.h:62
httpmock::TestEnvironment::TestEnvironment
TestEnvironment()
Definition: test_environment.h:23
mock_holder.h
httpmock::TestEnvironment::TearDown
virtual void TearDown()
Definition: test_environment.h:46
httpmock::TestEnvironment::getMock
const HTTPMock & getMock() const
Definition: test_environment.h:50
port_searcher.h
httpmock::TestEnvironment::SetUp
virtual void SetUp()
Definition: test_environment.h:40
httpmock::TestEnvironment::TestEnvironment
TestEnvironment(HTTPMock &&mock)
Definition: test_environment.h:30