exportarr/internal/client/client_test.go
Jack b8b7cc6be3
Feature/support bazarr (#214)
* remove the branch requirement to run tests

feature/support-bazarr

* lint makefile

feature/support-bazarr

* add bazarr to readme

feature/support-bazarr

* also comment out release image as we only want tests to happen

feature/support-bazarr

* initial changes to start attempting to pull bazarr i thinnk?

feature/support-bazarr

* make it only run the tests, but skip release image

feature/support-bazarr

* add a genric makefile to build container localy, run and test

feature/support-bazarr

* rename build command

feature/support-bazarr

* add pic and grafana dashboard adjustments

feature/support-bazarr

* refactor metrics to just be contained within 1 class

* add logic to allow csv values to params

* remove some todos

* make api version optional

feature/support-bazarr

* adjust makefile to auto kill and start

feature/support-bazarr

* remove now invalid test

feature/support-bazarr

* add a test for no api version instead

feature/support-bazarr

* add a test to enforce csv params moving forward

feature/support-bazarr

* add saml payloads for all endpoints, add test that mocks and verifies result

feature/support-bazarr

* Update .env.dist

* Update .gitignore

* Update internal/client/client.go

* add a tidy task

---------

Co-authored-by: Devin Buhl <onedr0p@users.noreply.github.com>
2023-10-08 02:32:59 -04:00

111 lines
2.9 KiB
Go

package client
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewClient(t *testing.T) {
u := "http://localhost"
require := require.New(t)
c, err := NewClient(u, true, nil)
require.NoError(err, "NewClient should not return an error")
require.NotNil(c, "NewClient should return a client")
require.Equal(u, c.URL.String(), "NewClient should set the correct URL")
require.True(c.httpClient.Transport.(*ExportarrTransport).inner.(*http.Transport).TLSClientConfig.InsecureSkipVerify)
}
// Need tests for FormAuth & BasicAuth
func TestDoRequest(t *testing.T) {
parameters := []struct {
name string
endpoint string
queryParams map[string]string
expectedURL string
}{
{
name: "noParams",
endpoint: "queue",
expectedURL: "/queue",
},
{
name: "params",
endpoint: "test",
queryParams: map[string]string{
"page": "1",
"testParam": "asdf",
},
expectedURL: "/test?page=1&testParam=asdf",
},
{
name: "csv params",
endpoint: "test",
queryParams: map[string]string{
"ids[]": "1,2,1234",
"testParam": "asdf",
},
expectedURL: "/test?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=1234&testParam=asdf",
},
}
for _, param := range parameters {
t.Run(param.name, func(t *testing.T) {
require := require.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(param.expectedURL, r.URL.String(), "DoRequest should use the correct URL")
fmt.Fprintln(w, "{\"test\": \"asdf2\"}")
}))
defer ts.Close()
target := struct {
Test string `json:"test"`
}{}
expected := target
expected.Test = "asdf2"
client, err := NewClient(ts.URL, false, nil)
if err != nil {
panic(err)
}
require.Nil(err, "NewClient should not return an error")
require.NotNil(client, "NewClient should return a client")
err = client.DoRequest(param.endpoint, &target, param.queryParams)
require.Nil(err, "DoRequest should not return an error: %s", err)
require.Equal(expected, target, "DoRequest should return the correct data")
})
}
}
func TestDoRequest_PanicRecovery(t *testing.T) {
require := require.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ret := struct {
TestField string
TestField2 string
}{
TestField: "asdf",
TestField2: "asdf2",
}
s, err := json.Marshal(ret)
require.NoError(err)
w.Write(s)
w.WriteHeader(http.StatusOK)
return
}))
defer ts.Close()
client, err := NewClient(ts.URL, false, nil)
require.Nil(err, "NewClient should not return an error")
require.NotNil(client, "NewClient should return a client")
err = client.DoRequest("test", nil)
require.NotPanics(func() {
require.Error(err, "DoRequest should return an error: %s", err)
}, "DoRequest should recover from a panic")
}