summaryrefslogtreecommitdiff
path: root/lib/common_test.go
blob: 71d55cc5f1332704eba59b5e18e513d6fb89a680 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package lib

import (
	"io"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestGetRemoteURLContent(t *testing.T) {
	t.Run("success", func(t *testing.T) {
		s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusOK)
			_, _ = w.Write([]byte("hello"))
		}))
		defer s.Close()

		data, err := GetRemoteURLContent(s.URL)
		if err != nil {
			t.Fatalf("GetRemoteURLContent() error = %v", err)
		}
		if string(data) != "hello" {
			t.Fatalf("GetRemoteURLContent() = %s, want %s", data, "hello")
		}
	})

	t.Run("status error", func(t *testing.T) {
		s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusBadGateway)
		}))
		defer s.Close()

		if _, err := GetRemoteURLContent(s.URL); err == nil {
			t.Fatalf("expected error for non-200 response")
		}
	})

	t.Run("request error", func(t *testing.T) {
		if _, err := GetRemoteURLContent("http://[%"); err == nil {
			t.Fatalf("expected error for invalid URL")
		}
	})
}

func TestGetRemoteURLReader(t *testing.T) {
	t.Run("success", func(t *testing.T) {
		s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusOK)
			_, _ = w.Write([]byte("world"))
		}))
		defer s.Close()

		rc, err := GetRemoteURLReader(s.URL)
		if err != nil {
			t.Fatalf("GetRemoteURLReader() error = %v", err)
		}
		defer rc.Close()

		data, err := io.ReadAll(rc)
		if err != nil {
			t.Fatalf("unexpected read error: %v", err)
		}
		if string(data) != "world" {
			t.Fatalf("GetRemoteURLReader() = %s, want %s", data, "world")
		}
	})

	t.Run("status error", func(t *testing.T) {
		s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusBadRequest)
		}))
		defer s.Close()

		if rc, err := GetRemoteURLReader(s.URL); err == nil {
			rc.Close()
			t.Fatalf("expected error for non-200 response")
		}
	})

	t.Run("request error", func(t *testing.T) {
		if _, err := GetRemoteURLReader("http://[%"); err == nil {
			t.Fatalf("expected error for invalid URL")
		}
	})
}

func TestGetIgnoreIPType(t *testing.T) {
	if fn := GetIgnoreIPType(IPv4); fn == nil || fn() != IPv6 {
		t.Fatalf("GetIgnoreIPType(IPv4) = %v", fn)
	}
	if fn := GetIgnoreIPType(IPv6); fn == nil || fn() != IPv4 {
		t.Fatalf("GetIgnoreIPType(IPv6) = %v", fn)
	}
	if fn := GetIgnoreIPType(IPType("other")); fn != nil {
		t.Fatalf("GetIgnoreIPType(other) = %v, want nil", fn)
	}
}

func TestWantedListExtendedUnmarshalJSON(t *testing.T) {
	t.Run("slice input", func(t *testing.T) {
		var w WantedListExtended
		if err := w.UnmarshalJSON([]byte(`["a","b"]`)); err != nil {
			t.Fatalf("UnmarshalJSON() error = %v", err)
		}
		if len(w.TypeSlice) != 2 || w.TypeSlice[0] != "a" || w.TypeSlice[1] != "b" {
			t.Fatalf("TypeSlice = %#v", w.TypeSlice)
		}
		if len(w.TypeMap) != 0 {
			t.Fatalf("TypeMap should be empty, got %#v", w.TypeMap)
		}
	})

	t.Run("map input", func(t *testing.T) {
		var w WantedListExtended
		if err := w.UnmarshalJSON([]byte(`{"x":["y"]}`)); err != nil {
			t.Fatalf("UnmarshalJSON() error = %v", err)
		}
		if len(w.TypeSlice) != 0 {
			t.Fatalf("TypeSlice should be empty, got %#v", w.TypeSlice)
		}
		if got := w.TypeMap["x"]; len(got) != 1 || got[0] != "y" {
			t.Fatalf("TypeMap = %#v", w.TypeMap)
		}
	})

	t.Run("invalid input", func(t *testing.T) {
		var w WantedListExtended
		if err := w.UnmarshalJSON([]byte(`123`)); err == nil {
			t.Fatalf("expected error for invalid json")
		}
	})

	t.Run("empty input", func(t *testing.T) {
		var w WantedListExtended
		if err := w.UnmarshalJSON([]byte(``)); err != nil {
			t.Fatalf("UnmarshalJSON() error = %v", err)
		}
	})
}