server.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include <library/cpp/http/misc/parsed_request.h>
  3. #include <library/cpp/http/server/http.h>
  4. #include <library/cpp/http/server/response.h>
  5. #include <library/cpp/testing/unittest/tests_data.h>
  6. #include <util/system/event.h>
  7. #include <functional>
  8. class TAutoEvent;
  9. class TThread;
  10. namespace NMock {
  11. class TMockServer {
  12. public:
  13. using TGenerator = std::function<TRequestReplier*()>;
  14. TMockServer(ui16 port, TGenerator generator);
  15. TMockServer(const THttpServerOptions& options, TGenerator generator);
  16. ~TMockServer();
  17. size_t GetClientCount() const;
  18. void SetGenerator(TGenerator generator);
  19. private:
  20. static void* Worker(void* arg);
  21. class TCallBack;
  22. THolder<TCallBack> Cb_;
  23. THolder<THttpServer> Server_;
  24. THolder<TThread> Thread_;
  25. THolder<TAutoEvent> Ev_;
  26. };
  27. class TPong: public TRequestReplier {
  28. public:
  29. bool DoReply(const TReplyParams& params) override {
  30. const TParsedHttpFull parsed(params.Input.FirstLine());
  31. const HttpCodes code = parsed.Path == "/ping" ? HTTP_OK : HTTP_NOT_FOUND;
  32. THttpResponse resp(code);
  33. resp.OutTo(params.Output);
  34. return true;
  35. }
  36. };
  37. }