Skip to content

Commit

Permalink
Check for valid json returned from elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
jessjenkins committed Nov 8, 2019
1 parent 6663c25 commit 05bccc9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -191,6 +192,13 @@ func SearchHandlerFunc(elasticSearchClient ElasticSearcher) http.HandlerFunc {
http.Error(w, "Failed to run search query", http.StatusInternalServerError)
return
}

if json.Valid([]byte(responseData)) != true {
log.Debug("Invlid JSON returned by elastic search for search query", nil)
http.Error(w, "Failed to process search query", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.Write(responseData)
}
Expand Down
22 changes: 22 additions & 0 deletions api/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ func TestSearchHandlerFunc(t *testing.T) {
So(actualRequest, ShouldContainSubstring, "term=a")
})

Convey("Should return InternalError for invalid json from elastic search", t, func() {
setupSearchTestTemplates("term={{.Term}};")
esMock := &ElasticSearcherMock{
MultiSearchFunc: func(index string, docType string, request []byte) ([]byte, error) {
return []byte(`{"dummy":"response"`), nil
},
}

searchHandler := SearchHandlerFunc(esMock)

req := httptest.NewRequest("GET", "http://localhost:8080/search?term=a", nil)
resp := httptest.NewRecorder()

searchHandler.ServeHTTP(resp, req)

So(resp.Code, ShouldEqual, http.StatusInternalServerError)
So(resp.Body.String(), ShouldContainSubstring, "Failed to process search query")
So(esMock.MultiSearchCalls(), ShouldHaveLength, 1)
actualRequest := string(esMock.MultiSearchCalls()[0].Request)
So(actualRequest, ShouldContainSubstring, "term=a")
})

Convey("Should return OK for valid search result", t, func() {
setupSearchTestTemplates("term={{.Term}};")
esMock := &ElasticSearcherMock{
Expand Down
7 changes: 7 additions & 0 deletions api/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"bytes"
"encoding/json"
"net/http"
"strings"
"text/template"
Expand Down Expand Up @@ -45,6 +46,12 @@ func TimeseriesLookupHandlerFunc(elasticSearchClient ElasticSearcher) http.Handl
return
}

if json.Valid([]byte(responseData)) != true {
log.Debug("Invlid JSON returned by elastic search for timeseries query", nil)
http.Error(w, "Failed to process timeseries query", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.Write(responseData)
}
Expand Down
23 changes: 23 additions & 0 deletions api/timeseries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ func TestTimeseriesLookupHandlerFunc(t *testing.T) {
So(actualRequest, ShouldContainSubstring, "Cdid=a")
})

Convey("Should return InternalError for invalid json from elastic search", t, func() {
setupTimeseriesTestTemplates("Cdid={{.Cdid}}")
esMock := &ElasticSearcherMock{
SearchFunc: func(index string, docType string, request []byte) ([]byte, error) {
return []byte(`{"dummy":"response"`), nil
},
}

timeSeriesHandler := TimeseriesLookupHandlerFunc(esMock)

req := httptest.NewRequest("GET", "http://localhost:8080/timeseries/a", nil)
req = mux.SetURLVars(req, map[string]string{"cdid": "a"})
resp := httptest.NewRecorder()

timeSeriesHandler.ServeHTTP(resp, req)

So(resp.Code, ShouldEqual, http.StatusInternalServerError)
So(resp.Body.String(), ShouldContainSubstring, "Failed to process timeseries query")
So(esMock.SearchCalls(), ShouldHaveLength, 1)
actualRequest := string(esMock.SearchCalls()[0].Request)
So(actualRequest, ShouldContainSubstring, "Cdid=a")
})

Convey("Should return OK for valid search result", t, func() {
setupTimeseriesTestTemplates("Cdid={{.Cdid}}")
esMock := &ElasticSearcherMock{
Expand Down

0 comments on commit 05bccc9

Please sign in to comment.