1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74

content / public / test / slow_http_response.h [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_TEST_SLOW_HTTP_RESPONSE_H_
#define CONTENT_PUBLIC_TEST_SLOW_HTTP_RESPONSE_H_

#include <string>

#include "base/memory/ref_counted.h"
#include "base/strings/string_split.h"
#include "base/task/sequenced_task_runner.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

namespace content {

// An HTTP response that may not complete ever.
class SlowHttpResponse : public net::test_server::HttpResponse {
  using HttpResponseDelegate = net::test_server::HttpResponseDelegate;

 public:
  // Test URLs.
  static const char kSlowResponseUrl[];
  static const char kFinishSlowResponseUrl[];
  static const int kFirstResponsePartSize;
  static const int kSecondResponsePartSize;

  // Callback run once the request has been received that allows the test to
  // start and then later finish the response at the time of its choosing. It
  // need not run either callback, but `start_response` must be run before
  // `finish_response`.
  using GotRequestCallback =
      base::OnceCallback<void(base::OnceClosure start_response,
                              base::OnceClosure finish_response)>;

  // Helper to make a `got_request` callback for when the SlowHttpResponse
  // should actually finish immediately.
  static GotRequestCallback FinishResponseImmediately();
  // Helper to make a `got_request` callback for when the SlowHttpResponse
  // should not reply at all.
  static GotRequestCallback NoResponse();

  // If `url` is `kSlowResponseUrl` this constructs an HttpResponse that will
  // not complete until the closure given to `got_request_callback` is run.
  explicit SlowHttpResponse(GotRequestCallback got_request);
  ~SlowHttpResponse() override;

  SlowHttpResponse(const SlowHttpResponse&) = delete;
  SlowHttpResponse& operator=(const SlowHttpResponse&) = delete;

  // Subclasses can override this method to add custom HTTP response headers.
  // These headers are only applied to the slow response itself, not the
  // response to |kFinishSlowResponseUrl|.
  virtual base::StringPairs ResponseHeaders();

  // Subclasses can override this method to write a custom status line; the
  // default implementation sets a 200 OK response. This status code is applied
  // only to the slow response itself, not the response to
  // |kFinishSlowResponseUrl|.
  virtual std::pair<net::HttpStatusCode, std::string> StatusLine();

  // net::test_server::HttpResponse implementations.
  void SendResponse(base::WeakPtr<HttpResponseDelegate> delegate) override;

 private:
  scoped_refptr<base::SequencedTaskRunner> main_thread_;
  GotRequestCallback got_request_;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_TEST_SLOW_HTTP_RESPONSE_H_