-
Notifications
You must be signed in to change notification settings - Fork 29
/
suite.go
43 lines (37 loc) · 974 Bytes
/
suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package mockusso
import (
"net/http"
"net/http/httptest"
"github.com/juju/qthttptest"
)
// Server represents a mock USSO server.
type Server struct {
MockUSSO *Handler
server *httptest.Server
saved http.RoundTripper
}
// NewServer starts a mock USSO server and also modifies
// http.DefaultTransport to redirect requests addressed to
// https://login.ubuntu.com to it.
//
// The returned Server must be closed after use.
func NewServer() *Server {
s := &Server{
MockUSSO: New("https://login.ubuntu.com"),
}
s.server = httptest.NewServer(s.MockUSSO)
rt := qthttptest.URLRewritingTransport{
MatchPrefix: "https://login.ubuntu.com",
Replace: s.server.URL,
RoundTripper: http.DefaultTransport,
}
s.saved = http.DefaultTransport
http.DefaultTransport = rt
return s
}
func (s *Server) Close() {
http.DefaultTransport = s.saved
s.server.Close()
}