File: web_resource_response_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 7,388 bytes parent folder | download | duplicates (5)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/embedder_support/android/util/web_resource_response.h"

#include <map>
#include <memory>
#include <optional>
#include <string>

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "components/embedder_support/android/util/input_stream.h"
#include "net/http/http_response_headers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/jni_zero/default_conversions.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/embedder_support/android/native_j_unittests_jni_headers/WebResourceResponseUnittest_jni.h"

namespace embedder_support {
class WebResourceResponseTest : public testing::Test {
 public:
  WebResourceResponseTest() = default;

 protected:
  void SetUp() override {
    env_ = jni_zero::AttachCurrentThread();
    ASSERT_THAT(env_, testing::NotNull());
  }

  std::multimap<std::string, std::string> HeadersToMap(
      const net::HttpResponseHeaders* headers) {
    std::multimap<std::string, std::string> result;
    size_t iter = 0;
    std::string name;
    std::string value;
    while (headers->EnumerateHeaderLines(&iter, &name, &value)) {
      result.insert({name, value});
    }
    return result;
  }

  raw_ptr<JNIEnv> env_;
};

TEST_F(WebResourceResponseTest, TestNullJniConversion) {
  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_getNullResponse(env_);
  EXPECT_FALSE(response);
}

TEST_F(WebResourceResponseTest, CanReadAllFields) {
  std::string mime_type = "text/plain";
  std::string charset = "utf-8";
  int status_code = 217;
  std::string reason_phrase = "Reason Overridden";
  base::flat_map<std::string, std::string> headers{
      {"X-Header", "value1"},
      {"Y-Header", "value2"},
  };
  std::string body = "Body Content";

  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_createJavaObject(
          env_, mime_type, charset, status_code, reason_phrase, headers, body);
  EXPECT_TRUE(response);

  std::string received_mime_type;
  EXPECT_TRUE(response->GetMimeType(env_, &received_mime_type));
  EXPECT_EQ(mime_type, received_mime_type);

  std::string received_charset;
  EXPECT_TRUE(response->GetCharset(env_, &received_charset));
  EXPECT_EQ(charset, received_charset);

  int received_status_code;
  std::string received_reason_phrase;
  EXPECT_TRUE(response->GetStatusInfo(env_, &received_status_code,
                                      &received_reason_phrase));
  EXPECT_EQ(status_code, received_status_code);
  EXPECT_EQ(reason_phrase, received_reason_phrase);

  scoped_refptr<net::HttpResponseHeaders> received_headers =
      base::MakeRefCounted<net::HttpResponseHeaders>("");
  EXPECT_TRUE(response->GetResponseHeaders(env_, received_headers.get()));
  std::multimap<std::string, std::string> received_header_map =
      HeadersToMap(received_headers.get());
  EXPECT_EQ(2UL, received_header_map.size());
  EXPECT_EQ(1UL, received_header_map.count("X-Header"));
  EXPECT_EQ("value1", received_header_map.find("X-Header")->second);
  EXPECT_EQ(1UL, received_header_map.count("Y-Header"));
  EXPECT_EQ("value2", received_header_map.find("Y-Header")->second);

  EXPECT_TRUE(response->HasInputStream(env_));
  std::unique_ptr<InputStream> input_stream = response->GetInputStream(env_);
  EXPECT_TRUE(input_stream);

  int received_bytes_available;
  input_stream->BytesAvailable(&received_bytes_available);
  EXPECT_EQ(body.length(), (size_t)received_bytes_available);
}

TEST_F(WebResourceResponseTest, HandlesNullHeaderMap) {
  // Construct a response with a null header map.
  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_createJavaObject(
          env_, "text/plain", "utf-8", 200, "OK", "body content");

  scoped_refptr<net::HttpResponseHeaders> received_headers =
      base::MakeRefCounted<net::HttpResponseHeaders>("");
  EXPECT_FALSE(response->GetResponseHeaders(env_, received_headers.get()));
  std::multimap<std::string, std::string> received_header_map =
      HeadersToMap(received_headers.get());
  EXPECT_EQ(0UL, received_header_map.size())
      << "No headers should be added to the HttpResponseHeaders";
}

TEST_F(WebResourceResponseTest, IgnoresLowStatusCodes) {
  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_createJavaObject(
          env_, "text/plain", "utf-8", 50, "OK", "body content");

  int received_status_code = -1;
  std::string received_reason_phrase = "unchanged";
  EXPECT_FALSE(response->GetStatusInfo(env_, &received_status_code,
                                       &received_reason_phrase));
  EXPECT_EQ(-1, received_status_code) << "No change to variable value expected";
  EXPECT_EQ("unchanged", received_reason_phrase)
      << "No change to variable value expected";
}

TEST_F(WebResourceResponseTest, IgnoresHighStatusCodes) {
  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_createJavaObject(
          env_, "text/plain", "utf-8", 600, "OK", "body content");

  int received_status_code = -1;
  std::string received_reason_phrase = "unchanged";
  EXPECT_FALSE(response->GetStatusInfo(env_, &received_status_code,
                                       &received_reason_phrase));
  EXPECT_EQ(-1, received_status_code) << "No change to variable value expected";
  EXPECT_EQ("unchanged", received_reason_phrase)
      << "No change to variable value expected";
}

TEST_F(WebResourceResponseTest, IgnoresUnsetReasonPhrase) {
  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_createJavaObject(
          env_, "text/plain", "utf-8", 200, std::nullopt, "body content");

  int received_status_code = -1;
  std::string received_reason_phrase = "unchanged";
  EXPECT_FALSE(response->GetStatusInfo(env_, &received_status_code,
                                       &received_reason_phrase));
  EXPECT_EQ(-1, received_status_code) << "No change to variable value expected";
  EXPECT_EQ("unchanged", received_reason_phrase)
      << "No change to variable value expected";
}

TEST_F(WebResourceResponseTest, IgnoresUnsetMimeType) {
  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_createJavaObject(
          env_, std::nullopt, "utf-8", 200, "OK", "body content");

  std::string received_mime_type = "unchanged";
  EXPECT_FALSE(response->GetMimeType(env_, &received_mime_type));
  EXPECT_EQ("unchanged", received_mime_type)
      << "No change to variable value expected";
}

TEST_F(WebResourceResponseTest, IgnoresUnsetCharset) {
  std::unique_ptr<WebResourceResponse> response =
      Java_WebResourceResponseUnittest_createJavaObject(
          env_, "text/plain", std::nullopt, 200, "OK", "body content");

  std::string received_charset = "unchanged";
  EXPECT_FALSE(response->GetCharset(env_, &received_charset));
  EXPECT_EQ("unchanged", received_charset)
      << "No change to variable value expected";
}

}  // namespace embedder_support