summaryrefslogtreecommitdiff
path: root/main_test.go
blob: 48ba1e9cb63863774f817ec4f47c51ea60a90dc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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},
		{"empty source", "http://", "https://my.site/part/target", errSrcInvalid},
		{"target no accepted", "https://example.org/somewhere", "wrong.site/tgt", errTgtNotAccepted},
		{"wrong source scheme", "ftp://example.org/somewhere", "http://my.site/part/tgt", errInvalidScheme},
		{"wrong target scheme", "http://example.org/somewhere", "ssh://my.site/part/tgt", errInvalidScheme},
	}

	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))
			}
		})
	}
}