From 7c36fb78a729dcce174a5d1e577edeeeb9ca806d Mon Sep 17 00:00:00 2001 From: Kegsay Date: Mon, 15 Jun 2020 16:57:59 +0100 Subject: Fix rooms v3 url paths for good - with tests (#1130) * Fix rooms v3 url paths for good - with tests - Add a test rig around `federationapi` to test routing. - Use `JSONVerifier` over `KeyRing` so we can stub things out more easily. - Add `test.NopJSONVerifier` which verifies nothing. - Add `base.BaseMux` which is the original `mux.Router` used to spawn public/internal routers. - Listen on `base.BaseMux` and not the default serve mux as it cleans paths which we don't want. - Factor out `ListenAndServe` to `test.ListenAndServe` and add flag for listening on TLS. * Fix comments * Linting --- userapi/userapi_test.go | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) (limited to 'userapi') diff --git a/userapi/userapi_test.go b/userapi/userapi_test.go index 423a8612..a4616363 100644 --- a/userapi/userapi_test.go +++ b/userapi/userapi_test.go @@ -3,16 +3,15 @@ package userapi_test import ( "context" "fmt" - "net" "net/http" "reflect" - "sync" "testing" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/internal/httputil" + "github.com/matrix-org/dendrite/internal/test" "github.com/matrix-org/dendrite/userapi" "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/inthttp" @@ -99,7 +98,7 @@ func TestQueryProfile(t *testing.T) { t.Run("HTTP API", func(t *testing.T) { router := mux.NewRouter().PathPrefix(httputil.InternalPathPrefix).Subrouter() userapi.AddInternalRoutes(router, userAPI) - apiURL, cancel := listenAndServe(t, router) + apiURL, cancel := test.ListenAndServe(t, router, false) defer cancel() httpAPI, err := inthttp.NewUserAPIClient(apiURL, &http.Client{}) if err != nil { @@ -111,28 +110,3 @@ func TestQueryProfile(t *testing.T) { runCases(userAPI) }) } - -func listenAndServe(t *testing.T, router *mux.Router) (apiURL string, cancel func()) { - listener, err := net.Listen("tcp", ":0") - if err != nil { - t.Fatalf("failed to listen: %s", err) - } - port := listener.Addr().(*net.TCPAddr).Port - srv := http.Server{} - - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - srv.Handler = router - err := srv.Serve(listener) - if err != nil && err != http.ErrServerClosed { - t.Logf("Listen failed: %s", err) - } - }() - - return fmt.Sprintf("http://localhost:%d", port), func() { - srv.Shutdown(context.Background()) - wg.Wait() - } -} -- cgit v1.2.3