github.com/rochacon/deis@v1.0.2-0.20150903015341-6839b592a1ff/Godeps/_workspace/src/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  	"encoding/json"
    10  	"fmt"
    11  	"io/ioutil"
    12  	"net/http"
    13  	"net/url"
    14  	"reflect"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  type SetOpaqueTest struct {
    20  	in             *url.URL
    21  	wantRequestURI string
    22  }
    23  
    24  var setOpaqueTests = []SetOpaqueTest{
    25  	// no path
    26  	{
    27  		&url.URL{
    28  			Scheme: "http",
    29  			Host:   "www.golang.org",
    30  		},
    31  		"http://www.golang.org",
    32  	},
    33  	// path
    34  	{
    35  		&url.URL{
    36  			Scheme: "http",
    37  			Host:   "www.golang.org",
    38  			Path:   "/",
    39  		},
    40  		"http://www.golang.org/",
    41  	},
    42  	// file with hex escaping
    43  	{
    44  		&url.URL{
    45  			Scheme: "https",
    46  			Host:   "www.golang.org",
    47  			Path:   "/file%20one&two",
    48  		},
    49  		"https://www.golang.org/file%20one&two",
    50  	},
    51  	// query
    52  	{
    53  		&url.URL{
    54  			Scheme:   "http",
    55  			Host:     "www.golang.org",
    56  			Path:     "/",
    57  			RawQuery: "q=go+language",
    58  		},
    59  		"http://www.golang.org/?q=go+language",
    60  	},
    61  	// file with hex escaping in path plus query
    62  	{
    63  		&url.URL{
    64  			Scheme:   "https",
    65  			Host:     "www.golang.org",
    66  			Path:     "/file%20one&two",
    67  			RawQuery: "q=go+language",
    68  		},
    69  		"https://www.golang.org/file%20one&two?q=go+language",
    70  	},
    71  	// query with hex escaping
    72  	{
    73  		&url.URL{
    74  			Scheme:   "http",
    75  			Host:     "www.golang.org",
    76  			Path:     "/",
    77  			RawQuery: "q=go%20language",
    78  		},
    79  		"http://www.golang.org/?q=go%20language",
    80  	},
    81  }
    82  
    83  // prefixTmpl is a template for the expected prefix of the output of writing
    84  // an HTTP request.
    85  const prefixTmpl = "GET %v HTTP/1.1\r\nHost: %v\r\n"
    86  
    87  func TestSetOpaque(t *testing.T) {
    88  	for _, test := range setOpaqueTests {
    89  		u := *test.in
    90  		SetOpaque(&u)
    91  
    92  		w := &bytes.Buffer{}
    93  		r := &http.Request{URL: &u}
    94  		if err := r.Write(w); err != nil {
    95  			t.Errorf("write request: %v", err)
    96  			continue
    97  		}
    98  
    99  		prefix := fmt.Sprintf(prefixTmpl, test.wantRequestURI, test.in.Host)
   100  		if got := string(w.Bytes()); !strings.HasPrefix(got, prefix) {
   101  			t.Errorf("got %q expected prefix %q", got, prefix)
   102  		}
   103  	}
   104  }
   105  
   106  type ExpandTest struct {
   107  	in         string
   108  	expansions map[string]string
   109  	want       string
   110  }
   111  
   112  var expandTests = []ExpandTest{
   113  	// no expansions
   114  	{
   115  		"http://www.golang.org/",
   116  		map[string]string{},
   117  		"http://www.golang.org/",
   118  	},
   119  	// one expansion, no escaping
   120  	{
   121  		"http://www.golang.org/{bucket}/delete",
   122  		map[string]string{
   123  			"bucket": "red",
   124  		},
   125  		"http://www.golang.org/red/delete",
   126  	},
   127  	// one expansion, with hex escapes
   128  	{
   129  		"http://www.golang.org/{bucket}/delete",
   130  		map[string]string{
   131  			"bucket": "red/blue",
   132  		},
   133  		"http://www.golang.org/red%2Fblue/delete",
   134  	},
   135  	// one expansion, with space
   136  	{
   137  		"http://www.golang.org/{bucket}/delete",
   138  		map[string]string{
   139  			"bucket": "red or blue",
   140  		},
   141  		"http://www.golang.org/red%20or%20blue/delete",
   142  	},
   143  	// expansion not found
   144  	{
   145  		"http://www.golang.org/{object}/delete",
   146  		map[string]string{
   147  			"bucket": "red or blue",
   148  		},
   149  		"http://www.golang.org//delete",
   150  	},
   151  	// multiple expansions
   152  	{
   153  		"http://www.golang.org/{one}/{two}/{three}/get",
   154  		map[string]string{
   155  			"one":   "ONE",
   156  			"two":   "TWO",
   157  			"three": "THREE",
   158  		},
   159  		"http://www.golang.org/ONE/TWO/THREE/get",
   160  	},
   161  	// utf-8 characters
   162  	{
   163  		"http://www.golang.org/{bucket}/get",
   164  		map[string]string{
   165  			"bucket": "£100",
   166  		},
   167  		"http://www.golang.org/%C2%A3100/get",
   168  	},
   169  	// punctuations
   170  	{
   171  		"http://www.golang.org/{bucket}/get",
   172  		map[string]string{
   173  			"bucket": `/\@:,.`,
   174  		},
   175  		"http://www.golang.org/%2F%5C%40%3A%2C./get",
   176  	},
   177  	// mis-matched brackets
   178  	{
   179  		"http://www.golang.org/{bucket/get",
   180  		map[string]string{
   181  			"bucket": "red",
   182  		},
   183  		"http://www.golang.org/{bucket/get",
   184  	},
   185  	// "+" prefix for suppressing escape
   186  	// See also: http://tools.ietf.org/html/rfc6570#section-3.2.3
   187  	{
   188  		"http://www.golang.org/{+topic}",
   189  		map[string]string{
   190  			"topic": "/topics/myproject/mytopic",
   191  		},
   192  		// The double slashes here look weird, but it's intentional
   193  		"http://www.golang.org//topics/myproject/mytopic",
   194  	},
   195  }
   196  
   197  func TestExpand(t *testing.T) {
   198  	for i, test := range expandTests {
   199  		u := url.URL{
   200  			Path: test.in,
   201  		}
   202  		Expand(&u, test.expansions)
   203  		got := u.Path
   204  		if got != test.want {
   205  			t.Errorf("got %q expected %q in test %d", got, test.want, i+1)
   206  		}
   207  	}
   208  }
   209  
   210  type CheckResponseTest struct {
   211  	in       *http.Response
   212  	bodyText string
   213  	want     error
   214  	errText  string
   215  }
   216  
   217  var checkResponseTests = []CheckResponseTest{
   218  	{
   219  		&http.Response{
   220  			StatusCode: http.StatusOK,
   221  		},
   222  		"",
   223  		nil,
   224  		"",
   225  	},
   226  	{
   227  		&http.Response{
   228  			StatusCode: http.StatusInternalServerError,
   229  		},
   230  		`{"error":{}}`,
   231  		&Error{
   232  			Code: http.StatusInternalServerError,
   233  			Body: `{"error":{}}`,
   234  		},
   235  		`googleapi: got HTTP response code 500 with body: {"error":{}}`,
   236  	},
   237  	{
   238  		&http.Response{
   239  			StatusCode: http.StatusNotFound,
   240  		},
   241  		`{"error":{"message":"Error message for StatusNotFound."}}`,
   242  		&Error{
   243  			Code:    http.StatusNotFound,
   244  			Message: "Error message for StatusNotFound.",
   245  			Body:    `{"error":{"message":"Error message for StatusNotFound."}}`,
   246  		},
   247  		"googleapi: Error 404: Error message for StatusNotFound.",
   248  	},
   249  	{
   250  		&http.Response{
   251  			StatusCode: http.StatusBadRequest,
   252  		},
   253  		`{"error":"invalid_token","error_description":"Invalid Value"}`,
   254  		&Error{
   255  			Code: http.StatusBadRequest,
   256  			Body: `{"error":"invalid_token","error_description":"Invalid Value"}`,
   257  		},
   258  		`googleapi: got HTTP response code 400 with body: {"error":"invalid_token","error_description":"Invalid Value"}`,
   259  	},
   260  	{
   261  		&http.Response{
   262  			StatusCode: http.StatusBadRequest,
   263  		},
   264  		`{"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}}`,
   265  		&Error{
   266  			Code: http.StatusBadRequest,
   267  			Errors: []ErrorItem{
   268  				{
   269  					Reason:  "keyInvalid",
   270  					Message: "Bad Request",
   271  				},
   272  			},
   273  			Body:    `{"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}}`,
   274  			Message: "Bad Request",
   275  		},
   276  		"googleapi: Error 400: Bad Request, keyInvalid",
   277  	},
   278  }
   279  
   280  func TestCheckResponse(t *testing.T) {
   281  	for _, test := range checkResponseTests {
   282  		res := test.in
   283  		if test.bodyText != "" {
   284  			res.Body = ioutil.NopCloser(strings.NewReader(test.bodyText))
   285  		}
   286  		g := CheckResponse(res)
   287  		if !reflect.DeepEqual(g, test.want) {
   288  			t.Errorf("CheckResponse: got %v, want %v", g, test.want)
   289  			gotJson, err := json.Marshal(g)
   290  			if err != nil {
   291  				t.Error(err)
   292  			}
   293  			wantJson, err := json.Marshal(test.want)
   294  			if err != nil {
   295  				t.Error(err)
   296  			}
   297  			t.Errorf("json(got):  %q\njson(want): %q", string(gotJson), string(wantJson))
   298  		}
   299  		if g != nil && g.Error() != test.errText {
   300  			t.Errorf("CheckResponse: unexpected error message.\nGot:  %q\nwant: %q", g, test.errText)
   301  		}
   302  	}
   303  }
   304  
   305  type VariantPoint struct {
   306  	Type        string
   307  	Coordinates []float64
   308  }
   309  
   310  type VariantTest struct {
   311  	in     map[string]interface{}
   312  	result bool
   313  	want   VariantPoint
   314  }
   315  
   316  var coords = []interface{}{1.0, 2.0}
   317  
   318  var variantTests = []VariantTest{
   319  	{
   320  		in: map[string]interface{}{
   321  			"type":        "Point",
   322  			"coordinates": coords,
   323  		},
   324  		result: true,
   325  		want: VariantPoint{
   326  			Type:        "Point",
   327  			Coordinates: []float64{1.0, 2.0},
   328  		},
   329  	},
   330  	{
   331  		in: map[string]interface{}{
   332  			"type":  "Point",
   333  			"bogus": coords,
   334  		},
   335  		result: true,
   336  		want: VariantPoint{
   337  			Type: "Point",
   338  		},
   339  	},
   340  }
   341  
   342  func TestVariantType(t *testing.T) {
   343  	for _, test := range variantTests {
   344  		if g := VariantType(test.in); g != test.want.Type {
   345  			t.Errorf("VariantType(%v): got %v, want %v", test.in, g, test.want.Type)
   346  		}
   347  	}
   348  }
   349  
   350  func TestConvertVariant(t *testing.T) {
   351  	for _, test := range variantTests {
   352  		g := VariantPoint{}
   353  		r := ConvertVariant(test.in, &g)
   354  		if r != test.result {
   355  			t.Errorf("ConvertVariant(%v): got %v, want %v", test.in, r, test.result)
   356  		}
   357  		if !reflect.DeepEqual(g, test.want) {
   358  			t.Errorf("ConvertVariant(%v): got %v, want %v", test.in, g, test.want)
   359  		}
   360  	}
   361  }