Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit c001a83

Browse files
jeffkaufmancrowell
authored andcommitted
Unit test ApacheFetch and ApacheWriter.
* pulls ApacheFetch out into it's own file * creates a set of mock apache functions that log to a global varible * extends NullCondvarCapableMutex to support condvars * comments on, but does not fix, bug in cookie stripping
1 parent e59644f commit c001a83

17 files changed

Lines changed: 1342 additions & 361 deletions

‎net/instaweb/apache/apache_writer.cc‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "net/instaweb/util/public/string_util.h"
2424

2525
#include "apr_strings.h" // for apr_pstrdup // NOLINT
26-
#include "httpd.h" // NOLINT
2726
#include "http_protocol.h" // NOLINT
2827

2928
namespace net_instaweb {
@@ -87,6 +86,12 @@ void ApacheWriter::OutputHeaders(ResponseHeaders* response_headers) {
8786
if (disable_downstream_header_filters_) {
8887
DisableDownstreamHeaderFilters(request_);
8988
}
89+
90+
// TODO(jefftk): Sanitize strips cookies and a lot of other headers. It's
91+
// being run after ResponseHeadersToApacheRequest(), however, which means it's
92+
// not actually doing anything. This code is not doing what it says it does,
93+
// and should be fixed, but it's possible some users like the mobilizing proxy
94+
// are depending on the current behavior.
9095
if (strip_cookies_ && response_headers->Sanitize()) {
9196
response_headers->ComputeCaching();
9297
}

‎net/instaweb/apache/apache_writer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "net/instaweb/util/public/basictypes.h"
2121
#include "net/instaweb/util/public/string_util.h"
2222
#include "net/instaweb/util/public/writer.h"
23-
#include "httpd.h" // NOLINT
2423

2524
struct request_rec;
2625

@@ -57,6 +56,7 @@ class ApacheWriter : public Writer {
5756

5857
// Removes 'Set-Cookie' and 'Set-Cookie2' from the response headers
5958
// once they are complete. Default is false.
59+
// TODO(jefftk): Doesn't actually do anything, because of an old bug.
6060
void set_strip_cookies(bool x) {
6161
strip_cookies_ = x;
6262
}

‎net/instaweb/apache/header_util.cc‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ void PrintHeaders(request_rec* request) {
153153
fflush(stdout);
154154
}
155155

156+
int StringAttributeCallback(void* rec, const char* key, const char* value) {
157+
GoogleString* out = static_cast<GoogleString*>(rec);
158+
out->append(key);
159+
out->append(": ");
160+
out->append(value);
161+
out->append("\n");
162+
return 1;
163+
}
164+
165+
GoogleString HeadersOutToString(request_rec* request) {
166+
GoogleString out;
167+
apr_table_do(StringAttributeCallback, &out, request->headers_out, NULL);
168+
return out;
169+
}
170+
171+
GoogleString SubprocessEnvToString(request_rec* request) {
172+
GoogleString out;
173+
apr_table_do(StringAttributeCallback, &out, request->subprocess_env, NULL);
174+
return out;
175+
}
176+
156177
class ApacheCachingHeaders : public CachingHeaders {
157178
public:
158179
explicit ApacheCachingHeaders(request_rec* request)

‎net/instaweb/apache/header_util.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ void DisableDownstreamHeaderFilters(request_rec* request);
8080
// Debug utility for printing Apache headers to stdout
8181
void PrintHeaders(request_rec* request);
8282

83+
// Get request->headers_out as a string, intended for tests.
84+
GoogleString HeadersOutToString(request_rec* request);
85+
86+
// Get request->subprocess_env as a string, intended for tests.
87+
GoogleString SubprocessEnvToString(request_rec* request);
88+
8389
// Updates headers related to caching (but not Cache-Control).
8490
void DisableCachingRelatedHeaders(request_rec* request);
8591

‎net/instaweb/apache/header_util_test.cc‎

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,15 @@
1515
// Author: jmarantz@google.com (Joshua Marantz)
1616

1717
#include "net/instaweb/apache/header_util.h"
18+
19+
#include "pagespeed/apache/mock_apache.h"
1820
#include "pagespeed/kernel/base/callback.h"
1921
#include "pagespeed/kernel/base/gtest.h"
2022
#include "pagespeed/kernel/http/http_names.h"
2123
#include "pagespeed/kernel/http/request_headers.h"
2224

23-
#include "apr_pools.h" // NOLINT
2425
#include "http_request.h" // NOLINT
2526

26-
// Provide stubs for functions normally resolved when loaded into
27-
// httpd, which we are not linking into the test.
28-
extern "C" {
29-
void ap_set_content_type(request_rec* request, char* type) {
30-
// Note that this is not the exact correct functionallity, but is
31-
// sufficient for the test.
32-
apr_table_set(request->headers_out,
33-
net_instaweb::HttpAttributes::kContentType,
34-
type);
35-
}
36-
37-
void ap_remove_output_filter(ap_filter_t* filter) {
38-
}
39-
}
40-
4127
namespace net_instaweb {
4228

4329
class HeaderUtilTest : public testing::Test {
@@ -48,15 +34,13 @@ class HeaderUtilTest : public testing::Test {
4834

4935
protected:
5036
virtual void SetUp() {
51-
apr_initialize();
52-
atexit(apr_terminate);
53-
apr_pool_create(&pool_, NULL);
54-
request_.headers_in = apr_table_make(pool_, 10);
55-
request_.headers_out = apr_table_make(pool_, 10);
37+
MockApache::Initialize();
38+
MockApache::PrepareRequest(&request_);
5639
}
5740

5841
virtual void TearDown() {
59-
apr_pool_destroy(pool_);
42+
MockApache::CleanupRequest(&request_);
43+
MockApache::Terminate();
6044
}
6145

6246
void SetLastModified(const char* last_modified) {
@@ -77,7 +61,6 @@ class HeaderUtilTest : public testing::Test {
7761
return apr_table_get(request_.headers_out, HttpAttributes::kCacheControl);
7862
}
7963

80-
apr_pool_t* pool_;
8164
request_rec request_;
8265
};
8366

0 commit comments

Comments
 (0)