func TestServicePrincipalTokenRefreshUnmarshals(t *testing.T) { spt := newServicePrincipalToken() expiresOn := strconv.Itoa(int(time.Now().Add(3600 * time.Second).Sub(expirationBase).Seconds())) j := newTokenJSON(expiresOn, "resource") resp := mocks.NewResponseWithContent(j) c := mocks.NewSender() s := autorest.DecorateSender(c, (func() autorest.SendDecorator { return func(s autorest.Sender) autorest.Sender { return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) { return resp, nil }) } })()) spt.SetSender(s) err := spt.Refresh() if err != nil { t.Errorf("azure: ServicePrincipalToken#Refresh returned an unexpected error (%v)", err) } else if spt.AccessToken != "accessToken" || spt.ExpiresIn != "3600" || spt.ExpiresOn != expiresOn || spt.NotBefore != expiresOn || spt.Resource != "resource" || spt.Type != "Bearer" { t.Errorf("azure: ServicePrincipalToken#Refresh failed correctly unmarshal the JSON -- expected %v, received %v", j, *spt) } }
func TestByUnmarhallingJSONIncludesJSONInErrors(t *testing.T) { v := &mocks.T{} j := jsonT[0 : len(jsonT)-2] r := mocks.NewResponseWithContent(j) err := Respond(r, ByUnmarshallingJSON(v), ByClosing()) if err == nil || !strings.Contains(err.Error(), j) { t.Errorf("autorest: ByUnmarshallingJSON failed to return JSON in error (%v)", err) } }
func TestLoggingInspectorByInspecting(t *testing.T) { b := bytes.Buffer{} c := Client{} li := LoggingInspector{Logger: log.New(&b, "", 0)} c.ResponseInspector = li.ByInspecting() Respond(mocks.NewResponseWithContent("Content"), c.ByInspecting()) if len(b.String()) <= 0 { t.Error("autorest: LoggingInspector#ByInspection did not record Response to the log") } }
func TestByUnmarhallingJSON(t *testing.T) { v := &mocks.T{} r := mocks.NewResponseWithContent(jsonT) err := Respond(r, ByUnmarshallingJSON(v), ByClosing()) if err != nil { t.Errorf("autorest: ByUnmarshallingJSON failed (%v)", err) } if v.Name != "Rob Pike" || v.Age != 42 { t.Errorf("autorest: ByUnmarshallingJSON failed to properly unmarshal") } }
func TestLoggingInspectorByInspectingRestoresBody(t *testing.T) { b := bytes.Buffer{} c := Client{} r := mocks.NewResponseWithContent("Content") li := LoggingInspector{Logger: log.New(&b, "", 0)} c.ResponseInspector = li.ByInspecting() Respond(r, c.ByInspecting()) s, _ := ioutil.ReadAll(r.Body) if len(s) <= 0 { t.Error("autorest: LoggingInspector#ByInspecting did not restore the Response body") } }
func ExampleByUnmarshallingJSON() { c := ` { "name" : "Rob Pike", "age" : 42 } ` type V struct { Name string `json:"name"` Age int `json:"age"` } v := &V{} Respond(mocks.NewResponseWithContent(c), ByUnmarshallingJSON(v), ByClosing()) fmt.Printf("%s is %d years old\n", v.Name, v.Age) // Output: Rob Pike is 42 years old }