forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup: common funcs for setting headers (ooni#1332)
I was trying to make sure the code was using a new definition of headers based on ArchivalMaybeBinaryString, but I stumbled upon a roadblock where there were too many code paths settings headers. I did not feel comfortable changing multiple code paths, so here is instead a diff that unifies setting headers. The slightly tricky part of this diff has been the requirement that we preserve headers' capitalization for hhfm. The catch seems to be that we should not use http.Header for setting the headers in hhfm, but rather pass through map[string][]string, which is not attached case normalization setters. I think we're fine in this department because we already had test cases and I added a bunch more test cases. After this diff is merged, I can resume with my plan to make headers use ArchivalMaybeBinaryString, which, in turn, is functional to automatically scrub headers, which is something we would like to happen in light of the introduction of more happy eyeballs code due to ooni/probe#2531.
- Loading branch information
1 parent
6daa1e6
commit 7d6b30b
Showing
6 changed files
with
225 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package hhfm | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestNewHeadersFromMap(t *testing.T) { | ||
|
||
// testcase is a test case run by this func | ||
type testcase struct { | ||
name string | ||
input map[string]string | ||
expect map[string][]string | ||
} | ||
|
||
cases := []testcase{{ | ||
name: "with nil input", | ||
input: nil, | ||
expect: http.Header{}, | ||
}, { | ||
name: "with empty input", | ||
input: map[string]string{}, | ||
expect: http.Header{}, | ||
}, { | ||
name: "common case: headers with mixed casing should be preserved", | ||
input: map[string]string{ | ||
"ConTent-TyPe": "text/html; charset=utf-8", | ||
"ViA": "a", | ||
"User-AgeNt": "miniooni/0.1.0", | ||
}, | ||
expect: map[string][]string{ | ||
"ConTent-TyPe": {"text/html; charset=utf-8"}, | ||
"ViA": {"a"}, | ||
"User-AgeNt": {"miniooni/0.1.0"}, | ||
}, | ||
}} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := newHeadersFromMap(tc.input) | ||
if diff := cmp.Diff(tc.expect, got); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.