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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
package lib
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetRemoteURLContent_Success(t *testing.T) {
// Create test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("test content"))
}))
defer server.Close()
content, err := GetRemoteURLContent(server.URL)
if err != nil {
t.Fatalf("GetRemoteURLContent failed: %v", err)
}
if string(content) != "test content" {
t.Errorf("GetRemoteURLContent = %s, want 'test content'", string(content))
}
}
func TestGetRemoteURLContent_NotFound(t *testing.T) {
// Create test server that returns 404
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
_, err := GetRemoteURLContent(server.URL)
if err == nil {
t.Error("GetRemoteURLContent should fail for 404")
}
}
func TestGetRemoteURLContent_InvalidURL(t *testing.T) {
_, err := GetRemoteURLContent("http://invalid-url-that-does-not-exist.local")
if err == nil {
t.Error("GetRemoteURLContent should fail for invalid URL")
}
}
func TestGetRemoteURLReader_Success(t *testing.T) {
// Create test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("test content"))
}))
defer server.Close()
reader, err := GetRemoteURLReader(server.URL)
if err != nil {
t.Fatalf("GetRemoteURLReader failed: %v", err)
}
defer reader.Close()
buf := make([]byte, 1024)
n, _ := reader.Read(buf)
if string(buf[:n]) != "test content" {
t.Errorf("GetRemoteURLReader content = %s, want 'test content'", string(buf[:n]))
}
}
func TestGetRemoteURLReader_NotFound(t *testing.T) {
// Create test server that returns 404
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
_, err := GetRemoteURLReader(server.URL)
if err == nil {
t.Error("GetRemoteURLReader should fail for 404")
}
}
func TestGetRemoteURLReader_InvalidURL(t *testing.T) {
_, err := GetRemoteURLReader("http://invalid-url-that-does-not-exist.local")
if err == nil {
t.Error("GetRemoteURLReader should fail for invalid URL")
}
}
func TestWantedListExtended_UnmarshalJSON_Slice(t *testing.T) {
jsonData := []byte(`["item1", "item2", "item3"]`)
var w WantedListExtended
err := json.Unmarshal(jsonData, &w)
if err != nil {
t.Fatalf("UnmarshalJSON failed: %v", err)
}
if len(w.TypeSlice) != 3 {
t.Errorf("len(TypeSlice) = %d, want 3", len(w.TypeSlice))
}
if len(w.TypeMap) != 0 {
t.Errorf("len(TypeMap) = %d, want 0", len(w.TypeMap))
}
expectedSlice := []string{"item1", "item2", "item3"}
for i, v := range w.TypeSlice {
if v != expectedSlice[i] {
t.Errorf("TypeSlice[%d] = %s, want %s", i, v, expectedSlice[i])
}
}
}
func TestWantedListExtended_UnmarshalJSON_Map(t *testing.T) {
jsonData := []byte(`{"key1": ["value1", "value2"], "key2": ["value3"]}`)
var w WantedListExtended
err := json.Unmarshal(jsonData, &w)
if err != nil {
t.Fatalf("UnmarshalJSON failed: %v", err)
}
if len(w.TypeSlice) != 0 {
t.Errorf("len(TypeSlice) = %d, want 0", len(w.TypeSlice))
}
if len(w.TypeMap) != 2 {
t.Errorf("len(TypeMap) = %d, want 2", len(w.TypeMap))
}
if len(w.TypeMap["key1"]) != 2 {
t.Errorf("len(TypeMap[key1]) = %d, want 2", len(w.TypeMap["key1"]))
}
if len(w.TypeMap["key2"]) != 1 {
t.Errorf("len(TypeMap[key2]) = %d, want 1", len(w.TypeMap["key2"]))
}
}
func TestWantedListExtended_UnmarshalJSON_EmptyData(t *testing.T) {
// Test calling UnmarshalJSON directly with empty data
var w WantedListExtended
err := w.UnmarshalJSON([]byte{})
if err != nil {
t.Fatalf("UnmarshalJSON with empty data failed: %v", err)
}
if len(w.TypeSlice) != 0 {
t.Errorf("len(TypeSlice) = %d, want 0", len(w.TypeSlice))
}
if len(w.TypeMap) != 0 {
t.Errorf("len(TypeMap) = %d, want 0", len(w.TypeMap))
}
}
func TestWantedListExtended_UnmarshalJSON_Invalid(t *testing.T) {
// Invalid JSON that is neither slice nor map
jsonData := []byte(`123`)
var w WantedListExtended
err := json.Unmarshal(jsonData, &w)
if err == nil {
t.Error("UnmarshalJSON should fail for invalid format")
}
}
func TestWantedListExtended_UnmarshalJSON_EmptySlice(t *testing.T) {
jsonData := []byte(`[]`)
var w WantedListExtended
err := json.Unmarshal(jsonData, &w)
if err != nil {
t.Fatalf("UnmarshalJSON failed: %v", err)
}
if len(w.TypeSlice) != 0 {
t.Errorf("len(TypeSlice) = %d, want 0", len(w.TypeSlice))
}
}
func TestWantedListExtended_UnmarshalJSON_EmptyMap(t *testing.T) {
jsonData := []byte(`{}`)
var w WantedListExtended
err := json.Unmarshal(jsonData, &w)
if err != nil {
t.Fatalf("UnmarshalJSON failed: %v", err)
}
// Empty object is a valid map
if len(w.TypeMap) != 0 {
t.Errorf("len(TypeMap) = %d, want 0", len(w.TypeMap))
}
}
|