| 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 | 16 |
| 18 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
| 19 ) | 18 ) |
| 20 | 19 |
| 21 func TestFetchServiceInfo(t *testing.T) { | 20 func TestFetchServiceInfo(t *testing.T) { |
| 22 Convey("Works", t, func() { | 21 Convey("Works", t, func() { |
| 23 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { | 22 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr
iter, r *http.Request) { |
| 24 w.Write([]byte(`{ | 23 w.Write([]byte(`{ |
| 25 "app_id": "some-app-id", | 24 "app_id": "some-app-id", |
| 26 "app_runtime": "go", | 25 "app_runtime": "go", |
| (...skipping 19 matching lines...) Expand all Loading... |
| 46 })) | 45 })) |
| 47 info, err := FetchServiceInfo(context.Background(), ts.URL) | 46 info, err := FetchServiceInfo(context.Background(), ts.URL) |
| 48 So(info, ShouldBeNil) | 47 So(info, ShouldBeNil) |
| 49 So(err, ShouldNotBeNil) | 48 So(err, ShouldNotBeNil) |
| 50 }) | 49 }) |
| 51 } | 50 } |
| 52 | 51 |
| 53 func TestInstallHandlers(t *testing.T) { | 52 func TestInstallHandlers(t *testing.T) { |
| 54 Convey("Works", t, func() { | 53 Convey("Works", t, func() { |
| 55 c := context.Background() | 54 c := context.Background() |
| 56 » » router := httprouter.New() | 55 » » r := router.New() |
| 57 returnErr := false | 56 returnErr := false |
| 58 | 57 |
| 59 » » InstallHandlers(router, middleware.TestingBase(c), func(context.
Context) (ServiceInfo, error) { | 58 » » InstallHandlers(r, router.MiddlewareChain{ |
| 59 » » » func(ctx *router.Context, next router.Handler) { |
| 60 » » » » ctx.Context = c |
| 61 » » » » next(ctx) |
| 62 » » » }, |
| 63 » » }, func(context.Context) (ServiceInfo, error) { |
| 60 if returnErr { | 64 if returnErr { |
| 61 return ServiceInfo{}, errors.New("fail") | 65 return ServiceInfo{}, errors.New("fail") |
| 62 } | 66 } |
| 63 return ServiceInfo{ | 67 return ServiceInfo{ |
| 64 AppID: "some-app-id", | 68 AppID: "some-app-id", |
| 65 AppRuntime: "go", | 69 AppRuntime: "go", |
| 66 AppRuntimeVersion: "go1.5.1", | 70 AppRuntimeVersion: "go1.5.1", |
| 67 AppVersion: "1234-abcdef", | 71 AppVersion: "1234-abcdef", |
| 68 ServiceAccountName: "some-app-id@appspot.gservic
eaccount.com", | 72 ServiceAccountName: "some-app-id@appspot.gservic
eaccount.com", |
| 69 }, nil | 73 }, nil |
| 70 }) | 74 }) |
| 71 | 75 |
| 72 w := httptest.NewRecorder() | 76 w := httptest.NewRecorder() |
| 73 req, _ := http.NewRequest("GET", "/auth/api/v1/server/info", nil
) | 77 req, _ := http.NewRequest("GET", "/auth/api/v1/server/info", nil
) |
| 74 » » router.ServeHTTP(w, req) | 78 » » r.ServeHTTP(w, req) |
| 75 So(w.Code, ShouldEqual, 200) | 79 So(w.Code, ShouldEqual, 200) |
| 76 So(w.Body.String(), ShouldResemble, | 80 So(w.Body.String(), ShouldResemble, |
| 77 `{"app_id":"some-app-id","app_runtime":"go",`+ | 81 `{"app_id":"some-app-id","app_runtime":"go",`+ |
| 78 `"app_runtime_version":"go1.5.1",`+ | 82 `"app_runtime_version":"go1.5.1",`+ |
| 79 `"app_version":"1234-abcdef","service_account_na
me":`+ | 83 `"app_version":"1234-abcdef","service_account_na
me":`+ |
| 80 `"some-app-id@appspot.gserviceaccount.com"}`+"\n
") | 84 `"some-app-id@appspot.gserviceaccount.com"}`+"\n
") |
| 81 | 85 |
| 82 returnErr = true | 86 returnErr = true |
| 83 w = httptest.NewRecorder() | 87 w = httptest.NewRecorder() |
| 84 req, _ = http.NewRequest("GET", "/auth/api/v1/server/info", nil) | 88 req, _ = http.NewRequest("GET", "/auth/api/v1/server/info", nil) |
| 85 » » router.ServeHTTP(w, req) | 89 » » r.ServeHTTP(w, req) |
| 86 So(w.Code, ShouldEqual, 500) | 90 So(w.Code, ShouldEqual, 500) |
| 87 So(w.Body.String(), ShouldResemble, "{\"error\":\"fail\"}\n") | 91 So(w.Body.String(), ShouldResemble, "{\"error\":\"fail\"}\n") |
| 88 }) | 92 }) |
| 89 } | 93 } |
| OLD | NEW |