| 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 signing | 5 package signing |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "net/http" | |
| 10 "net/http/httptest" | 9 "net/http/httptest" |
| 11 "testing" | 10 "testing" |
| 12 | 11 |
| 13 "github.com/julienschmidt/httprouter" | |
| 14 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
| 15 | 13 |
| 16 » "github.com/luci/luci-go/server/middleware" | 14 » "github.com/luci/luci-go/server/router" |
| 17 | 15 |
| 18 . "github.com/luci/luci-go/common/testing/assertions" | 16 . "github.com/luci/luci-go/common/testing/assertions" |
| 19 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
| 20 ) | 18 ) |
| 21 | 19 |
| 22 func TestContext(t *testing.T) { | 20 func TestContext(t *testing.T) { |
| 23 Convey("Works", t, func() { | 21 Convey("Works", t, func() { |
| 24 ctx := context.Background() | 22 ctx := context.Background() |
| 25 | 23 |
| 26 So(GetSigner(ctx), ShouldBeNil) | 24 So(GetSigner(ctx), ShouldBeNil) |
| 27 _, _, err := SignBytes(ctx, nil) | 25 _, _, err := SignBytes(ctx, nil) |
| 28 So(err, ShouldErrLike, "no Signer in the context") | 26 So(err, ShouldErrLike, "no Signer in the context") |
| 29 | 27 |
| 30 ctx = SetSigner(ctx, phonySigner{}) | 28 ctx = SetSigner(ctx, phonySigner{}) |
| 31 _, _, err = SignBytes(ctx, nil) | 29 _, _, err = SignBytes(ctx, nil) |
| 32 So(err, ShouldBeNil) | 30 So(err, ShouldBeNil) |
| 33 }) | 31 }) |
| 34 } | 32 } |
| 35 | 33 |
| 36 func TestHandlers(t *testing.T) { | 34 func TestHandlers(t *testing.T) { |
| 37 | |
| 38 call := func(s Signer) (*PublicCertificates, error) { | 35 call := func(s Signer) (*PublicCertificates, error) { |
| 39 » » r := httprouter.New() | 36 » » r := router.New() |
| 40 » » InstallHandlers(r, func(h middleware.Handler) httprouter.Handle
{ | 37 » » InstallHandlers(r, router.MiddlewareChain{ |
| 41 » » » return func(w http.ResponseWriter, r *http.Request, p ht
tprouter.Params) { | 38 » » » func(c *router.Context, next router.Handler) { |
| 42 » » » » ctx := SetSigner(context.Background(), s) | 39 » » » » c.Context = SetSigner(context.Background(), s) |
| 43 » » » » h(ctx, w, r, p) | 40 » » » » next(c) |
| 44 » » » } | 41 » » » }, |
| 45 }) | 42 }) |
| 46 ts := httptest.NewServer(r) | 43 ts := httptest.NewServer(r) |
| 47 return FetchCertificates(context.Background(), ts.URL+"/auth/api
/v1/server/certificates") | 44 return FetchCertificates(context.Background(), ts.URL+"/auth/api
/v1/server/certificates") |
| 48 } | 45 } |
| 49 | 46 |
| 50 Convey("Works", t, func() { | 47 Convey("Works", t, func() { |
| 51 certs, err := call(phonySigner{}) | 48 certs, err := call(phonySigner{}) |
| 52 So(err, ShouldBeNil) | 49 So(err, ShouldBeNil) |
| 53 So(len(certs.Certificates), ShouldEqual, 1) | 50 So(len(certs.Certificates), ShouldEqual, 1) |
| 54 }) | 51 }) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 80 } | 77 } |
| 81 return &PublicCertificates{ | 78 return &PublicCertificates{ |
| 82 Certificates: []Certificate{ | 79 Certificates: []Certificate{ |
| 83 { | 80 { |
| 84 KeyName: "phonyKey", | 81 KeyName: "phonyKey", |
| 85 X509CertificatePEM: "phonyPEM", | 82 X509CertificatePEM: "phonyPEM", |
| 86 }, | 83 }, |
| 87 }, | 84 }, |
| 88 }, nil | 85 }, nil |
| 89 } | 86 } |
| OLD | NEW |