| 152 | } |
| 153 | |
| 154 | func searchHandler(req *http.Request) *http.Response { |
| 155 | apiKey := req.Header.Get("X-Api-Key") |
| 156 | if apiKey != validApiKey { |
| 157 | return &http.Response{ |
| 158 | StatusCode: http.StatusForbidden, |
| 159 | Body: nil, |
| 160 | Header: make(http.Header), |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | url, _ := url.Parse(req.URL.String()) |
| 165 | |
| 166 | ipsParam := url.Query().Get("ips") |
| 167 | if ipsParam == "" { |
| 168 | return &http.Response{ |
| 169 | StatusCode: http.StatusBadRequest, |
| 170 | Body: nil, |
| 171 | Header: make(http.Header), |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | totalIps := 0 |
| 176 | notFound := 0 |
| 177 | |
| 178 | ips := strings.Split(ipsParam, ",") |
| 179 | for _, ip := range ips { |
| 180 | _, ok := smokeResponses[ip] |
| 181 | if ok { |
| 182 | totalIps++ |
| 183 | } else { |
| 184 | notFound++ |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | response := fmt.Sprintf(`{"total": %d, "not_found": %d, "items": [`, totalIps, notFound) |
| 189 | for _, ip := range ips { |
| 190 | response += smokeResponses[ip] |
| 191 | } |
| 192 | |
| 193 | response += "]}" |
| 194 | reader := io.NopCloser(strings.NewReader(response)) |
| 195 | |
| 196 | return &http.Response{ |
| 197 | StatusCode: http.StatusOK, |
| 198 | Body: reader, |
| 199 | Header: make(http.Header), |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func TestBadFireAuth(t *testing.T) { |
| 204 | ctiClient := NewCrowdsecCTIClient(WithAPIKey("asdasd"), WithHTTPClient(&http.Client{ |