Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Unified Diff: server/auth/internal/testing.go

Issue 2386643003: auth: Make luci-go services trust signatures produced by the token server. (Closed)
Patch Set: add tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « server/auth/internal/fetch.go ('k') | server/auth/signing/signingtest/signer.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: server/auth/internal/testing.go
diff --git a/server/auth/internal/testing.go b/server/auth/internal/testing.go
new file mode 100644
index 0000000000000000000000000000000000000000..9e9b7aef12f63d59c4a751e23243c9d5f6c59f3f
--- /dev/null
+++ b/server/auth/internal/testing.go
@@ -0,0 +1,44 @@
+// Copyright 2015 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package internal
+
+import (
+ "bytes"
+ "io/ioutil"
+ "net/http"
+ "sync"
+
+ "golang.org/x/net/context"
+)
+
+var testTransportKey = "testTransport"
+
+// TestTransportCallback is used from unit tests.
+type TestTransportCallback func(r *http.Request, body string) (code int, response string)
+
+// WithTestTransport puts a testing transport in the context to use for fetches.
+func WithTestTransport(c context.Context, cb TestTransportCallback) context.Context {
+ return context.WithValue(c, &testTransportKey, &testTransport{cb: cb})
+}
+
+type testTransport struct {
+ lock sync.Mutex
+ cb TestTransportCallback
+}
+
+func (t *testTransport) RoundTrip(r *http.Request) (*http.Response, error) {
+ t.lock.Lock()
+ defer t.lock.Unlock()
+ body, err := ioutil.ReadAll(r.Body)
+ r.Body.Close()
+ if err != nil {
+ return nil, err
+ }
+ code, resp := t.cb(r, string(body))
+ return &http.Response{
+ StatusCode: code,
+ Body: ioutil.NopCloser(bytes.NewReader([]byte(resp))),
+ }, nil
+}
« no previous file with comments | « server/auth/internal/fetch.go ('k') | server/auth/signing/signingtest/signer.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698