Created
November 28, 2020 01:04
-
-
Save cyx/5092be8c879b9f33cb3744da1d8d0461 to your computer and use it in GitHub Desktop.
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
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