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