generated from matrix-org/.github
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out callback server stuff; update README
- Loading branch information
Showing
4 changed files
with
95 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package deploy | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/matrix-org/complement-crypto/internal/api" | ||
"github.com/matrix-org/complement/must" | ||
) | ||
|
||
type CallbackData struct { | ||
Method string `json:"method"` | ||
URL string `json:"url"` | ||
AccessToken string `json:"access_token"` | ||
ResponseCode int `json:"response_code"` | ||
} | ||
|
||
// NewCallbackServer runs a local HTTP server that can read callbacks from mitmproxy. | ||
// Returns the URL of the callback server for use with WithMITMOptions, along with a close function | ||
// which should be called when the test finishes to shut down the HTTP server. | ||
func NewCallbackServer(t *testing.T, cb func(CallbackData)) (callbackURL string, close func()) { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
var data CallbackData | ||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil { | ||
api.Errorf(t, "error decoding json: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
t.Logf("CallbackServer: %v %+v", time.Now(), data) | ||
cb(data) | ||
w.WriteHeader(200) | ||
}) | ||
// listen on a random high numbered port | ||
ln, err := net.Listen("tcp", ":0") //nolint | ||
must.NotError(t, "failed to listen on a tcp port", err) | ||
port := ln.Addr().(*net.TCPAddr).Port | ||
srv := http.Server{ | ||
Addr: fmt.Sprintf(":%d", port), | ||
Handler: mux, | ||
} | ||
go srv.Serve(ln) | ||
return fmt.Sprintf("http://host.docker.internal:%d", port), func() { | ||
srv.Close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters