Treat non-200 HTTP response as error.

Change-Id: I2a9f3b47012b3c4839be53aa44c66d16dd41a24a
This commit is contained in:
Bjoern Rabenstein 2014-06-16 17:04:08 +02:00
parent 9c81f2e82a
commit 8da3429e45
2 changed files with 21 additions and 0 deletions

View File

@ -224,6 +224,9 @@ func (t *target) scrape(timestamp clientmodel.Timestamp, ingester extraction.Ing
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("server returned HTTP status %s", resp.Status)
}
defer resp.Body.Close()
processor, err := extraction.ProcessorForRequestHeader(resp.Header)

View File

@ -14,6 +14,7 @@
package retrieval
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
@ -127,3 +128,20 @@ func TestTargetScrapeTimeout(t *testing.T) {
t.Fatal(err)
}
}
func TestTargetScrape404(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
testTarget := NewTarget(server.URL, 10*time.Millisecond, clientmodel.LabelSet{})
ingester := nopIngester{}
want := errors.New("server returned HTTP status 404 Not Found")
got := testTarget.Scrape(time.Now(), ingester)
if got == nil || want.Error() != got.Error() {
t.Fatalf("want err %q, got %q", want, got)
}
}