package main
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestSyncRejection(t *testing.T) {
server := httptest.NewServer(endpoint{"my.site/part"})
defer server.Close()
tests := []struct {
name string
source string
target string
expect string
}{
{"invalid source", "https||:example.org/somewhere", "my.site/part/target", errSrcInvalid},
{"target no accepted", "https://example.org/somewhere", "wrong.site/tgt", errTgtNotAccepted},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
client := http.DefaultClient
r, err := client.PostForm(server.URL, url.Values{
"source": []string{tc.source},
"target": []string{tc.target},
})
if err != nil {
t.Fatal(err)
}
if r.StatusCode != 400 {
t.Fatalf("want 400, got %v", r.Status)
}
bb, _ := io.ReadAll(r.Body)
if string(bb) != tc.expect {
t.Fatalf("want %s, got %s", tc.expect, string(bb))
}
})
}
}