Browse Source

Always check

Piotr Czajkowski 3 years ago
parent
commit
74fb5d2330
2 changed files with 30 additions and 0 deletions
  1. 5 0
      search.go
  2. 25 0
      search_test.go

+ 5 - 0
search.go

@@ -112,6 +112,11 @@ func (app Application) getResultsFromTM(tmURL string, tm TM, searchJSON []byte)
 		return true, tempResults
 	}
 
+	if resp.StatusCode != http.StatusOK {
+		log.Printf("Problem getting results (%s)!", resp.Status)
+		return false, tempResults
+	}
+
 	err = jsonDecoder(resp.Body, &tempResults)
 	if err != nil {
 		log.Printf("Error decoding results: %s", err)

+ 25 - 0
search_test.go

@@ -73,3 +73,28 @@ func TestSearch(t *testing.T) {
 		}
 	}
 }
+
+func TestSearchWrongStatus(t *testing.T) {
+	server := fakeServer(http.StatusBadRequest, "")
+	defer server.Close()
+
+	var app Application
+	app.setBaseURL(server.URL)
+
+	tmsJSON, err := os.Open("./testFiles/tms.json")
+	if err != nil {
+		t.Fatalf("Error reading tms: %s", err)
+	}
+	defer tmsJSON.Close()
+
+	var tms []TM
+	err = jsonDecoder(tmsJSON, &tms)
+	if err != nil {
+		t.Fatalf("Error decoding tms: %s", err)
+	}
+
+	searchResults := app.search(tms, "something")
+	if searchResults.TotalResults != 0 {
+		t.Fatal("There should be no results!")
+	}
+}