base_requests_server_unittest.cc 4.47 KB
Newer Older
1 2 3 4
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "google_apis/drive/base_requests.h"
6

7 8
#include <memory>

9
#include "base/bind.h"
10
#include "base/files/file_util.h"
11
#include "base/files/scoped_temp_dir.h"
12
#include "base/message_loop/message_loop.h"
13
#include "base/run_loop.h"
14 15 16 17
#include "google_apis/drive/dummy_auth_service.h"
#include "google_apis/drive/request_sender.h"
#include "google_apis/drive/task_util.h"
#include "google_apis/drive/test_util.h"
18
#include "net/test/embedded_test_server/embedded_test_server.h"
19 20
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
21 22 23 24 25 26 27 28 29 30 31
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace google_apis {

namespace {

const char kTestUserAgent[] = "test-user-agent";

}  // namespace

32
class BaseRequestsServerTest : public testing::Test {
33
 protected:
34
  BaseRequestsServerTest() {
35 36
  }

37
  void SetUp() override {
38
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
39 40

    request_context_getter_ = new net::TestURLRequestContextGetter(
41
        message_loop_.task_runner());
42

43 44 45
    request_sender_.reset(new RequestSender(
        new DummyAuthService,
        request_context_getter_.get(),
46
        message_loop_.task_runner(),
47
        kTestUserAgent));
48

49
    ASSERT_TRUE(test_server_.Start());
50
    test_server_.RegisterRequestHandler(
51
        base::Bind(&test_util::HandleDownloadFileRequest,
52 53 54 55 56
                   test_server_.base_url(),
                   base::Unretained(&http_request_)));
  }

  // Returns a temporary file path suitable for storing the cache file.
57
  base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) {
58
    return temp_dir_.GetPath().Append(file_name);
59 60
  }

61
  base::MessageLoopForIO message_loop_;  // Test server needs IO thread.
62
  net::EmbeddedTestServer test_server_;
63
  std::unique_ptr<RequestSender> request_sender_;
64
  scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
65
  base::ScopedTempDir temp_dir_;
66 67

  // The incoming HTTP request is saved so tests can verify the request
68
  // parameters like HTTP method (ex. some requests should use DELETE
69
  // instead of GET).
70
  net::test_server::HttpRequest http_request_;
71 72
};

73
TEST_F(BaseRequestsServerTest, DownloadFileRequest_ValidFile) {
74
  DriveApiErrorCode result_code = DRIVE_OTHER_ERROR;
75
  base::FilePath temp_file;
76 77
  {
    base::RunLoop run_loop;
78
    DownloadFileRequestBase* request = new DownloadFileRequestBase(
79 80 81 82 83 84
        request_sender_.get(),
        test_util::CreateQuitCallback(
            &run_loop,
            test_util::CreateCopyResultCallback(&result_code, &temp_file)),
        GetContentCallback(),
        ProgressCallback(),
85
        test_server_.GetURL("/files/drive/testfile.txt"),
86 87
        GetTestCachedFilePath(
            base::FilePath::FromUTF8Unsafe("cached_testfile.txt")));
88
    request_sender_->StartRequestWithAuthRetry(request);
89 90
    run_loop.Run();
  }
91

92
  std::string contents;
93
  base::ReadFileToString(temp_file, &contents);
94
  base::DeleteFile(temp_file, false);
95

96
  EXPECT_EQ(HTTP_SUCCESS, result_code);
97
  EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
98
  EXPECT_EQ("/files/drive/testfile.txt", http_request_.relative_url);
99

100
  const base::FilePath expected_path =
101
      test_util::GetTestFilePath("drive/testfile.txt");
102
  std::string expected_contents;
103
  base::ReadFileToString(expected_path, &expected_contents);
104 105 106
  EXPECT_EQ(expected_contents, contents);
}

107
TEST_F(BaseRequestsServerTest, DownloadFileRequest_NonExistentFile) {
108
  DriveApiErrorCode result_code = DRIVE_OTHER_ERROR;
109
  base::FilePath temp_file;
110 111
  {
    base::RunLoop run_loop;
112
    DownloadFileRequestBase* request = new DownloadFileRequestBase(
113 114 115 116 117 118
        request_sender_.get(),
        test_util::CreateQuitCallback(
            &run_loop,
            test_util::CreateCopyResultCallback(&result_code, &temp_file)),
        GetContentCallback(),
        ProgressCallback(),
119
        test_server_.GetURL("/files/gdata/no-such-file.txt"),
120 121
        GetTestCachedFilePath(
            base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt")));
122
    request_sender_->StartRequestWithAuthRetry(request);
123 124
    run_loop.Run();
  }
125
  EXPECT_EQ(HTTP_NOT_FOUND, result_code);
126
  EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
127
  EXPECT_EQ("/files/gdata/no-such-file.txt",
128
            http_request_.relative_url);
129 130 131 132
  // Do not verify the not found message.
}

}  // namespace google_apis