server_ut.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <library/cpp/testing/mock_server/server.h>
  2. #include <library/cpp/http/simple/http_client.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. Y_UNIT_TEST_SUITE(Server) {
  5. int i;
  6. Y_UNIT_TEST(pong) {
  7. TPortManager pm;
  8. ui16 port = pm.GetPort(80);
  9. NMock::TMockServer server(port, []() { return new NMock::TPong; });
  10. TKeepAliveHttpClient cl("localhost", port);
  11. UNIT_ASSERT_VALUES_EQUAL(200, cl.DoGet("/ping"));
  12. UNIT_ASSERT_VALUES_EQUAL(404, cl.DoGet("/kek"));
  13. }
  14. Y_UNIT_TEST(custom) {
  15. class TCustomReplier: public TRequestReplier {
  16. public:
  17. bool DoReply(const TReplyParams& params) override {
  18. THttpResponse resp(HttpCodes::HTTP_OK);
  19. resp.SetContent("everithing is ok");
  20. resp.OutTo(params.Output);
  21. return true;
  22. }
  23. };
  24. TPortManager pm;
  25. ui16 port = pm.GetPort(80);
  26. NMock::TMockServer server(port, []() { return new TCustomReplier; });
  27. TKeepAliveHttpClient cl("localhost", port);
  28. TStringStream out;
  29. UNIT_ASSERT_VALUES_EQUAL(200, cl.DoGet("/foo", &out));
  30. UNIT_ASSERT_VALUES_EQUAL("everithing is ok", out.Str());
  31. }
  32. }