...

Source file src/cloud.google.com/go/httpreplay/internal/proxy/converter_test.go

Documentation: cloud.google.com/go/httpreplay/internal/proxy

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"net/http"
    21  	"net/url"
    22  	"testing"
    23  
    24  	"cloud.google.com/go/internal/testutil"
    25  	"github.com/google/go-cmp/cmp"
    26  )
    27  
    28  func TestConvertRequest(t *testing.T) {
    29  	clone := func(h http.Header) http.Header {
    30  		h2 := http.Header{}
    31  		for k, v := range h {
    32  			h2[k] = v
    33  		}
    34  		return h2
    35  	}
    36  
    37  	body := []byte("hello")
    38  
    39  	conv := defaultConverter()
    40  	conv.registerClearParams("secret")
    41  	conv.registerRemoveParams("rm*")
    42  	url, err := url.Parse("https://www.example.com?a=1&rmx=x&secret=2&c=3&rmy=4")
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	in := &http.Request{
    47  		Method: "GET",
    48  		URL:    url,
    49  		Body:   io.NopCloser(bytes.NewReader(body)),
    50  		Header: http.Header{
    51  			"Content-Type":                      {"text/plain"},
    52  			"Authorization":                     {"oauth2-token"},
    53  			"X-Goog-Encryption-Key":             {"a-secret-key"},
    54  			"X-Goog-Copy-Source-Encryption-Key": {"another-secret-key"},
    55  		},
    56  	}
    57  	origHeader := clone(in.Header)
    58  
    59  	got, err := conv.convertRequest(in)
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	want := &Request{
    64  		Method:    "GET",
    65  		URL:       "https://www.example.com?a=1&secret=CLEARED&c=3",
    66  		MediaType: "text/plain",
    67  		BodyParts: [][]byte{body},
    68  		Header: http.Header{
    69  			"X-Goog-Encryption-Key":             {"CLEARED"},
    70  			"X-Goog-Copy-Source-Encryption-Key": {"CLEARED"},
    71  		},
    72  		Trailer: http.Header{},
    73  	}
    74  	if diff := cmp.Diff(got, want); diff != "" {
    75  		t.Error(diff)
    76  	}
    77  	// The original headers should be the same.
    78  	if got, want := in.Header, origHeader; !testutil.Equal(got, want) {
    79  		t.Errorf("got  %+v\nwant %+v", got, want)
    80  	}
    81  }
    82  
    83  func TestPattern(t *testing.T) {
    84  	for _, test := range []struct {
    85  		in, want string
    86  	}{
    87  		{"", "^$"},
    88  		{"abc", "^abc$"},
    89  		{"*ab*", "^.*ab.*$"},
    90  		{`a\*b`, `^a\\.*b$`},
    91  		{"***", "^.*.*.*$"},
    92  	} {
    93  		got := pattern(test.in).String()
    94  		if got != test.want {
    95  			t.Errorf("%q: got %s, want %s", test.in, got, test.want)
    96  		}
    97  	}
    98  }
    99  
   100  func TestScrubQuery(t *testing.T) {
   101  	clear := []tRegexp{pattern("c*")}
   102  	remove := []tRegexp{pattern("r*")}
   103  	for _, test := range []struct {
   104  		in, want string
   105  	}{
   106  		{"", ""},
   107  		{"a=1", "a=1"},
   108  		{"a=1&b=2;g=3", "a=1&b=2;g=3"},
   109  		{"a=1&r=2;c=3", "a=1&c=CLEARED"},
   110  		{"ra=1&rb=2&rc=3", ""},
   111  		{"a=1&%Z=2&r=3&c=4", "a=1&%Z=2&c=CLEARED"},
   112  	} {
   113  		got := scrubQuery(test.in, clear, remove)
   114  		if got != test.want {
   115  			t.Errorf("%s: got %q, want %q", test.in, got, test.want)
   116  		}
   117  	}
   118  }
   119  

View as plain text