go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/internal/fetch_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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 internal
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  	. "go.chromium.org/luci/common/testing/assertions"
    25  )
    26  
    27  func init() {
    28  	RegisterClientFactory(func(ctx context.Context, scopes []string) (*http.Client, error) {
    29  		return http.DefaultClient, nil
    30  	})
    31  }
    32  
    33  func TestFetch(t *testing.T) {
    34  	Convey("with test context", t, func(c C) {
    35  		body := ""
    36  		status := http.StatusOK
    37  		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    38  			w.WriteHeader(status)
    39  			w.Write([]byte(body))
    40  		}))
    41  		defer ts.Close()
    42  
    43  		ctx := context.Background()
    44  
    45  		Convey("fetch works", func() {
    46  			var val struct {
    47  				A string `json:"a"`
    48  			}
    49  			body = `{"a": "hello"}`
    50  			req := Request{
    51  				Method: "GET",
    52  				URL:    ts.URL,
    53  				Out:    &val,
    54  			}
    55  			So(req.Do(ctx), ShouldBeNil)
    56  			So(val.A, ShouldEqual, "hello")
    57  		})
    58  
    59  		Convey("handles bad status code", func() {
    60  			var val struct{}
    61  			status = http.StatusNotFound
    62  			req := Request{
    63  				Method: "GET",
    64  				URL:    ts.URL,
    65  				Out:    &val,
    66  			}
    67  			So(req.Do(ctx), ShouldErrLike, "HTTP code (404)")
    68  		})
    69  
    70  		Convey("handles bad body", func() {
    71  			var val struct{}
    72  			body = "not json"
    73  			req := Request{
    74  				Method: "GET",
    75  				URL:    ts.URL,
    76  				Out:    &val,
    77  			}
    78  			So(req.Do(ctx), ShouldErrLike, "can't deserialize JSON")
    79  		})
    80  
    81  		Convey("handles connection error", func() {
    82  			var val struct{}
    83  			req := Request{
    84  				Method: "GET",
    85  				URL:    "http://localhost:12345678",
    86  				Out:    &val,
    87  			}
    88  			So(req.Do(ctx), ShouldErrLike, "dial tcp")
    89  		})
    90  	})
    91  }