OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package info |
| 6 |
| 7 import ( |
| 8 "errors" |
| 9 "net/http" |
| 10 "net/http/httptest" |
| 11 "testing" |
| 12 |
| 13 "golang.org/x/net/context" |
| 14 |
| 15 "github.com/julienschmidt/httprouter" |
| 16 "github.com/luci/luci-go/server/middleware" |
| 17 |
| 18 . "github.com/smartystreets/goconvey/convey" |
| 19 ) |
| 20 |
| 21 func TestFetchServiceInfo(t *testing.T) { |
| 22 Convey("Works", t, func() { |
| 23 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { |
| 24 w.Write([]byte(`{ |
| 25 "app_id": "some-app-id", |
| 26 "app_runtime": "go", |
| 27 "app_runtime_version": "go1.5.1", |
| 28 "app_version": "1234-abcdef", |
| 29 "service_account_name": "some-app-id@appspot.gse
rviceaccount.com" |
| 30 }`)) |
| 31 })) |
| 32 info, err := FetchServiceInfo(context.Background(), ts.URL) |
| 33 So(err, ShouldBeNil) |
| 34 So(info, ShouldResemble, &ServiceInfo{ |
| 35 AppID: "some-app-id", |
| 36 AppRuntime: "go", |
| 37 AppRuntimeVersion: "go1.5.1", |
| 38 AppVersion: "1234-abcdef", |
| 39 ServiceAccountName: "some-app-id@appspot.gserviceaccount
.com", |
| 40 }) |
| 41 }) |
| 42 |
| 43 Convey("Error", t, func() { |
| 44 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { |
| 45 http.Error(w, "fail", http.StatusInternalServerError) |
| 46 })) |
| 47 info, err := FetchServiceInfo(context.Background(), ts.URL) |
| 48 So(info, ShouldBeNil) |
| 49 So(err, ShouldNotBeNil) |
| 50 }) |
| 51 } |
| 52 |
| 53 func TestInstallHandlers(t *testing.T) { |
| 54 Convey("Works", t, func() { |
| 55 c := context.Background() |
| 56 router := httprouter.New() |
| 57 returnErr := false |
| 58 |
| 59 InstallHandlers(router, middleware.TestingBase(c), func(context.
Context) (ServiceInfo, error) { |
| 60 if returnErr { |
| 61 return ServiceInfo{}, errors.New("fail") |
| 62 } |
| 63 return ServiceInfo{ |
| 64 AppID: "some-app-id", |
| 65 AppRuntime: "go", |
| 66 AppRuntimeVersion: "go1.5.1", |
| 67 AppVersion: "1234-abcdef", |
| 68 ServiceAccountName: "some-app-id@appspot.gservic
eaccount.com", |
| 69 }, nil |
| 70 }) |
| 71 |
| 72 w := httptest.NewRecorder() |
| 73 req, _ := http.NewRequest("GET", "/auth/api/v1/server/info", nil
) |
| 74 router.ServeHTTP(w, req) |
| 75 So(w.Code, ShouldEqual, 200) |
| 76 So(w.Body.String(), ShouldResemble, |
| 77 `{"app_id":"some-app-id","app_runtime":"go",`+ |
| 78 `"app_runtime_version":"go1.5.1",`+ |
| 79 `"app_version":"1234-abcdef","service_account_na
me":`+ |
| 80 `"some-app-id@appspot.gserviceaccount.com"}`+"\n
") |
| 81 |
| 82 returnErr = true |
| 83 w = httptest.NewRecorder() |
| 84 req, _ = http.NewRequest("GET", "/auth/api/v1/server/info", nil) |
| 85 router.ServeHTTP(w, req) |
| 86 So(w.Code, ShouldEqual, 500) |
| 87 So(w.Body.String(), ShouldResemble, "{\"error\":\"fail\"}\n") |
| 88 }) |
| 89 } |
OLD | NEW |