You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2007 Google Inc.
  2. // All Rights Reserved.
  3. #include "talk/base/gunit.h"
  4. #include "talk/base/httpserver.h"
  5. #include "talk/base/testutils.h"
  6. using namespace testing;
  7. namespace talk_base {
  8. namespace {
  9. const char* const kRequest =
  10. "GET /index.html HTTP/1.1\r\n"
  11. "Host: localhost\r\n"
  12. "\r\n";
  13. const char* const kResponse =
  14. "HTTP/1.1 200\r\n"
  15. "Connection: Close\r\n"
  16. "Content-Length: 0\r\n"
  17. "\r\n";
  18. struct HttpServerMonitor : public sigslot::has_slots<> {
  19. HttpServerTransaction* transaction;
  20. bool server_closed, connection_closed;
  21. HttpServerMonitor(HttpServer* server)
  22. : transaction(NULL), server_closed(false), connection_closed(false) {
  23. server->SignalCloseAllComplete.connect(this,
  24. &HttpServerMonitor::OnClosed);
  25. server->SignalHttpRequest.connect(this, &HttpServerMonitor::OnRequest);
  26. server->SignalHttpRequestComplete.connect(this,
  27. &HttpServerMonitor::OnRequestComplete);
  28. server->SignalConnectionClosed.connect(this,
  29. &HttpServerMonitor::OnConnectionClosed);
  30. }
  31. void OnRequest(HttpServer*, HttpServerTransaction* t) {
  32. ASSERT_FALSE(transaction);
  33. transaction = t;
  34. transaction->response.set_success();
  35. transaction->response.setHeader(HH_CONNECTION, "Close");
  36. }
  37. void OnRequestComplete(HttpServer*, HttpServerTransaction* t, int) {
  38. ASSERT_EQ(transaction, t);
  39. transaction = NULL;
  40. }
  41. void OnClosed(HttpServer*) {
  42. server_closed = true;
  43. }
  44. void OnConnectionClosed(HttpServer*, int, StreamInterface* stream) {
  45. connection_closed = true;
  46. delete stream;
  47. }
  48. };
  49. void CreateClientConnection(HttpServer& server,
  50. HttpServerMonitor& monitor,
  51. bool send_request) {
  52. StreamSource* client = new StreamSource;
  53. client->SetState(SS_OPEN);
  54. server.HandleConnection(client);
  55. EXPECT_FALSE(monitor.server_closed);
  56. EXPECT_FALSE(monitor.transaction);
  57. if (send_request) {
  58. // Simulate a request
  59. client->QueueString(kRequest);
  60. EXPECT_FALSE(monitor.server_closed);
  61. }
  62. }
  63. } // anonymous namespace
  64. TEST(HttpServer, DoesNotSignalCloseUnlessCloseAllIsCalled) {
  65. HttpServer server;
  66. HttpServerMonitor monitor(&server);
  67. // Add an active client connection
  68. CreateClientConnection(server, monitor, true);
  69. // Simulate a response
  70. ASSERT_TRUE(NULL != monitor.transaction);
  71. server.Respond(monitor.transaction);
  72. EXPECT_FALSE(monitor.transaction);
  73. // Connection has closed, but no server close signal
  74. EXPECT_FALSE(monitor.server_closed);
  75. EXPECT_TRUE(monitor.connection_closed);
  76. }
  77. TEST(HttpServer, SignalsCloseWhenNoConnectionsAreActive) {
  78. HttpServer server;
  79. HttpServerMonitor monitor(&server);
  80. // Add an idle client connection
  81. CreateClientConnection(server, monitor, false);
  82. // Perform graceful close
  83. server.CloseAll(false);
  84. // Connections have all closed
  85. EXPECT_TRUE(monitor.server_closed);
  86. EXPECT_TRUE(monitor.connection_closed);
  87. }
  88. TEST(HttpServer, SignalsCloseAfterGracefulCloseAll) {
  89. HttpServer server;
  90. HttpServerMonitor monitor(&server);
  91. // Add an active client connection
  92. CreateClientConnection(server, monitor, true);
  93. // Initiate a graceful close
  94. server.CloseAll(false);
  95. EXPECT_FALSE(monitor.server_closed);
  96. // Simulate a response
  97. ASSERT_TRUE(NULL != monitor.transaction);
  98. server.Respond(monitor.transaction);
  99. EXPECT_FALSE(monitor.transaction);
  100. // Connections have all closed
  101. EXPECT_TRUE(monitor.server_closed);
  102. EXPECT_TRUE(monitor.connection_closed);
  103. }
  104. TEST(HttpServer, SignalsCloseAfterForcedCloseAll) {
  105. HttpServer server;
  106. HttpServerMonitor monitor(&server);
  107. // Add an active client connection
  108. CreateClientConnection(server, monitor, true);
  109. // Initiate a forceful close
  110. server.CloseAll(true);
  111. // Connections have all closed
  112. EXPECT_TRUE(monitor.server_closed);
  113. EXPECT_TRUE(monitor.connection_closed);
  114. }
  115. } // namespace talk_base