mirror of https://github.com/matrix-org/go-neb.git
Browse Source
Add a mock HTTP client for Matrix clients to use. Add example test.
Add a mock HTTP client for Matrix clients to use. Add example test.
This will need rejigging at some point to make the test easier to set up.kegan/tests-prep
Kegan Dougal
8 years ago
4 changed files with 99 additions and 20 deletions
-
19src/github.com/matrix-org/go-neb/clients/clients.go
-
6src/github.com/matrix-org/go-neb/goneb.go
-
84src/github.com/matrix-org/go-neb/goneb_services_test.go
-
10src/github.com/matrix-org/go-neb/matrix/matrix.go
@ -1,31 +1,103 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"bytes" |
|||
"net/http" |
|||
"net/http/httptest" |
|||
"os" |
|||
"strconv" |
|||
"testing" |
|||
) |
|||
|
|||
var mux = http.NewServeMux() |
|||
|
|||
type MockTripper struct { |
|||
handlers map[string]func(req *http.Request) (*http.Response, error) |
|||
} |
|||
|
|||
func (rt MockTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
|||
key := req.Method + " " + req.URL.Path |
|||
h := rt.handlers[key] |
|||
if h == nil { |
|||
panic( |
|||
"Test RoundTrip: Unhandled request: " + key + "\n" + |
|||
"Handlers: " + strconv.Itoa(len(rt.handlers)), |
|||
) |
|||
} |
|||
return h(req) |
|||
} |
|||
|
|||
func (rt MockTripper) Handle(method, path string, handler func(req *http.Request) (*http.Response, error)) { |
|||
key := method + " " + path |
|||
if _, exists := rt.handlers[key]; exists { |
|||
panic("Test handler with key " + key + " already exists") |
|||
} |
|||
rt.handlers[key] = handler |
|||
} |
|||
|
|||
var tripper = MockTripper{make(map[string]func(req *http.Request) (*http.Response, error))} |
|||
|
|||
type nopCloser struct { |
|||
*bytes.Buffer |
|||
} |
|||
|
|||
func (nopCloser) Close() error { return nil } |
|||
|
|||
func newResponse(statusCode int, body string) *http.Response { |
|||
return &http.Response{ |
|||
StatusCode: statusCode, |
|||
Body: nopCloser{bytes.NewBufferString(body)}, |
|||
} |
|||
} |
|||
|
|||
func TestMain(m *testing.M) { |
|||
setup(envVars{ |
|||
BaseURL: "http://go.neb", |
|||
DatabaseType: "sqlite3", |
|||
DatabaseURL: ":memory:", |
|||
}, mux) |
|||
}, mux, &http.Client{ |
|||
Transport: tripper, |
|||
}) |
|||
exitCode := m.Run() |
|||
os.Exit(exitCode) |
|||
} |
|||
|
|||
func TestNotFound(t *testing.T) { |
|||
func TestConfigureClient(t *testing.T) { |
|||
for k := range tripper.handlers { |
|||
delete(tripper.handlers, k) |
|||
} |
|||
mockWriter := httptest.NewRecorder() |
|||
mockReq, _ := http.NewRequest("GET", "http://go.neb/foo", nil) |
|||
mux.ServeHTTP(mockWriter, mockReq) |
|||
tripper.Handle("POST", "/_matrix/client/r0/user/@link:hyrule/filter", |
|||
func(req *http.Request) (*http.Response, error) { |
|||
return newResponse(200, `{ |
|||
"filter_id":"abcdef" |
|||
}`), nil |
|||
}, |
|||
) |
|||
syncChan := make(chan string) |
|||
tripper.Handle("GET", "/_matrix/client/r0/sync", |
|||
func(req *http.Request) (*http.Response, error) { |
|||
syncChan <- "sync" |
|||
return newResponse(200, `{ |
|||
"next_batch":"11_22_33_44", |
|||
"rooms": {} |
|||
}`), nil |
|||
}, |
|||
) |
|||
|
|||
expectCode := 404 |
|||
mockReq, _ := http.NewRequest("POST", "http://go.neb/admin/configureClient", bytes.NewBufferString(` |
|||
{ |
|||
"UserID":"@link:hyrule", |
|||
"HomeserverURL":"http://hyrule.loz", |
|||
"AccessToken":"dangeroustogoalone", |
|||
"Sync":true, |
|||
"AutoJoinRooms":true |
|||
}`)) |
|||
mux.ServeHTTP(mockWriter, mockReq) |
|||
expectCode := 200 |
|||
if mockWriter.Code != expectCode { |
|||
t.Errorf("TestNotFound wanted HTTP status %d, got %d", expectCode, mockWriter.Code) |
|||
t.Errorf("TestConfigureClient wanted HTTP status %d, got %d", expectCode, mockWriter.Code) |
|||
} |
|||
|
|||
<-syncChan |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue