Use `go-httpbin` to run tests locally and avoid remote calls to `httpbin.org`

https://github.com/mccutchen/go-httpbin is MIT licensed and implements httpbin interfaces in Golang. 

Combining this package with `httptest.Server` allows client unit tests to run entirely locally with no dependency on remote calls to httpbin.org
This commit is contained in:
kramanathan01 2023-06-24 22:05:47 -07:00 committed by GitHub
parent fffa74f782
commit c3250257b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 0 deletions

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/lib/pq v1.10.9
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530
github.com/mccutchen/go-httpbin/v2 v2.9.1
github.com/prometheus/client_golang v1.16.0
github.com/rylans/getlang v0.0.0-20201227074721-9e7f44ff8aa0
github.com/tdewolff/minify/v2 v2.12.7

2
go.sum
View File

@ -33,6 +33,8 @@ github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mccutchen/go-httpbin/v2 v2.9.1 h1:blH3ecpqy7aQ9IHFjO9Xrmkfuv80rACnrg6M53gra8A=
github.com/mccutchen/go-httpbin/v2 v2.9.1/go.mod h1:+DBHcmg6EOeoizuiOI8iL12VIHXx+9YQNlz+gjB9uxk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/cachecontrol v0.1.0 h1:yJMy84ti9h/+OEWa752kBTKv4XC30OtVVHYv/8cTqKc=

View File

@ -0,0 +1,70 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package client // import "miniflux.app/http/client"
import (
"fmt"
"net/http/httptest"
"os"
"testing"
"github.com/mccutchen/go-httpbin/v2/httpbin"
)
var srv *httptest.Server
func TestMain(m *testing.M) {
srv = httptest.NewServer(httpbin.New())
exitCode := m.Run()
srv.Close()
os.Exit(exitCode)
}
func MakeClient(path string) *Client {
return New(fmt.Sprintf("%s%s", srv.URL, path))
}
func TestClientWithDelay(t *testing.T) {
clt := MakeClient("/delay/5")
clt.ClientTimeout = 1
_, err := clt.Get()
if err == nil {
t.Fatal(`The client should stops after 1 second`)
}
}
func TestClientWithError(t *testing.T) {
clt := MakeClient("/status/502")
clt.ClientTimeout = 5
response, err := clt.Get()
if err != nil {
t.Fatal(err)
}
if response.StatusCode != 502 {
t.Fatalf(`Unexpected response status code: %d`, response.StatusCode)
}
if !response.HasServerFailure() {
t.Fatal(`A 502 error is considered as server failure`)
}
}
func TestClientWithResponseTooLarge(t *testing.T) {
clt := MakeClient("/bytes/100")
clt.ClientMaxBodySize = 10
_, err := clt.Get()
if err == nil {
t.Fatal(`The client should fails when reading a response too large`)
}
}
func TestClientWithBasicAuth(t *testing.T) {
clt := MakeClient("/basic-auth/testuser/testpassword")
clt.WithCredentials("testuser", "testpassword")
_, err := clt.Get()
if err != nil {
t.Fatalf(`The client should be authenticated successfully: %v`, err)
}
}