github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/third_party/code.google.com/p/google-api-go-client/googleapi/googleapi_test.go (about) 1 // Copyright 2011 Google Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package googleapi 6 7 import ( 8 "bytes" 9 "fmt" 10 "net/http" 11 "net/url" 12 "strings" 13 "testing" 14 ) 15 16 type SetOpaqueTest struct { 17 in *url.URL 18 wantRequestURI string 19 } 20 21 var setOpaqueTests = []SetOpaqueTest{ 22 // no path 23 { 24 &url.URL{ 25 Scheme: "http", 26 Host: "www.golang.org", 27 }, 28 "http://www.golang.org", 29 }, 30 // path 31 { 32 &url.URL{ 33 Scheme: "http", 34 Host: "www.golang.org", 35 Path: "/", 36 }, 37 "http://www.golang.org/", 38 }, 39 // file with hex escaping 40 { 41 &url.URL{ 42 Scheme: "https", 43 Host: "www.golang.org", 44 Path: "/file%20one&two", 45 }, 46 "https://www.golang.org/file%20one&two", 47 }, 48 // query 49 { 50 &url.URL{ 51 Scheme: "http", 52 Host: "www.golang.org", 53 Path: "/", 54 RawQuery: "q=go+language", 55 }, 56 "http://www.golang.org/?q=go+language", 57 }, 58 // file with hex escaping in path plus query 59 { 60 &url.URL{ 61 Scheme: "https", 62 Host: "www.golang.org", 63 Path: "/file%20one&two", 64 RawQuery: "q=go+language", 65 }, 66 "https://www.golang.org/file%20one&two?q=go+language", 67 }, 68 // query with hex escaping 69 { 70 &url.URL{ 71 Scheme: "http", 72 Host: "www.golang.org", 73 Path: "/", 74 RawQuery: "q=go%20language", 75 }, 76 "http://www.golang.org/?q=go%20language", 77 }, 78 } 79 80 // prefixTmpl is a template for the expected prefix of the output of writing 81 // an HTTP request. 82 const prefixTmpl = "GET %v HTTP/1.1\r\nHost: %v\r\n" 83 84 func TestSetOpaque(t *testing.T) { 85 for _, test := range setOpaqueTests { 86 u := *test.in 87 SetOpaque(&u) 88 89 w := &bytes.Buffer{} 90 r := &http.Request{URL: &u} 91 if err := r.Write(w); err != nil { 92 t.Errorf("write request: %v", err) 93 continue 94 } 95 96 prefix := fmt.Sprintf(prefixTmpl, test.wantRequestURI, test.in.Host) 97 if got := string(w.Bytes()); !strings.HasPrefix(got, prefix) { 98 t.Errorf("got %q expected prefix %q", got, prefix) 99 } 100 } 101 }