Skip to content

Instantly share code, notes, and snippets.

@cyx
Created November 28, 2020 01:04
Show Gist options
  • Save cyx/5092be8c879b9f33cb3744da1d8d0461 to your computer and use it in GitHub Desktop.
Save cyx/5092be8c879b9f33cb3744da1d8d0461 to your computer and use it in GitHub Desktop.
package runtime
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestHTTP(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if want, got := http.MethodPost, r.Method; want != got {
t.Errorf("wanted method: %s, got %s", want, got)
}
if want, got := "request", string(reqBody); want != got {
t.Errorf("wanted req: %s, got %s", want, got)
}
w.Write([]byte("response"))
}))
defer srv.Close()
u, err := url.Parse(srv.URL)
if err != nil {
t.Fatal(err)
}
h := &HTTP{
URL: u,
httpClient: srv.Client(),
}
res, err := h.Execute(context.Background(), Request{Payload: []byte("request")})
if err != nil {
t.Fatal(err)
}
if want, got := "response", string(res.Payload); want != got {
t.Fatalf("wanted response: %s, got %s", want, got)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment