test_http_server.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include <library/cpp/http/misc/httpcodes.h>
  3. #include <util/generic/maybe.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/string.h>
  6. #include <functional>
  7. namespace NYql {
  8. class TTestHttpServer {
  9. public:
  10. struct TReply {
  11. int Code = 0;
  12. TString ETag;
  13. TString LastModified;
  14. TString Content;
  15. TMaybe<int> ContentLength;
  16. static TReply Ok(const TString& content, TMaybe<int> contentLength = {}) {
  17. TReply r;
  18. r.Code = HTTP_OK;
  19. r.Content = content;
  20. r.ContentLength = contentLength;
  21. return r;
  22. }
  23. static TReply OkETag(const TString& content, const TString& etag, TMaybe<int> contentLength = {}) {
  24. TReply r = Ok(content, contentLength);
  25. r.ETag = etag;
  26. return r;
  27. }
  28. static TReply OkLastModified(const TString& content, const TString& lastModified, TMaybe<int> contentLength = {}) {
  29. TReply r = Ok(content, contentLength);
  30. r.LastModified = lastModified;
  31. return r;
  32. }
  33. static TReply NotModified(const TString& etag = {}, const TString& lastModified = {}) {
  34. TReply r;
  35. r.Code = HTTP_NOT_MODIFIED;
  36. r.ETag = etag;
  37. r.LastModified = lastModified;
  38. return r;
  39. }
  40. static TReply Forbidden() {
  41. TReply r;
  42. r.Code = HTTP_FORBIDDEN;
  43. return r;
  44. }
  45. };
  46. struct TRequest {
  47. TString OAuthToken;
  48. TString IfNoneMatch;
  49. TString IfModifiedSince;
  50. };
  51. typedef std::function<TReply(const TRequest& request)> TRequestHandler;
  52. public:
  53. explicit TTestHttpServer(int port);
  54. ~TTestHttpServer();
  55. void Start();
  56. TString GetUrl() const;
  57. void SetRequestHandler(TRequestHandler handler);
  58. private:
  59. class TImpl;
  60. THolder<TImpl> Impl_;
  61. };
  62. }