Skip to content

Commit

Permalink
Merge 86487a2 into 79fd301
Browse files Browse the repository at this point in the history
  • Loading branch information
dencoded committed Nov 28, 2018
2 parents 79fd301 + 86487a2 commit d820690
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
95 changes: 95 additions & 0 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,101 @@ func TestAnalytics(t *testing.T) {
t.Error("Analytics record do not match", record)
}
})

t.Run("Detailed analytics", func(t *testing.T) {
defer resetTestConfig()
globalConf := config.Global()
globalConf.AnalyticsConfig.EnableDetailedRecording = true
config.SetGlobal(globalConf)

buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.Proxy.ListenPath = "/"
})

key := createSession()

authHeaders := map[string]string{
"authorization": key,
}

ts.Run(t, test.TestCase{
Path: "/", Headers: authHeaders, Code: 200,
})

// let records to to be sent
time.Sleep(recordsBufferFlushInterval + 50)

results := analytics.Store.GetAndDeleteSet(analyticsKeyName)
if len(results) != 1 {
t.Error("Should return 1 record: ", len(results))
}

var record AnalyticsRecord
msgpack.Unmarshal(results[0].([]byte), &record)
if record.ResponseCode != 200 {
t.Error("Analytics record do not match", record)
}

if record.RawRequest == "" {
t.Error("Detailed request info not found", record)
}

if record.RawResponse == "" {
t.Error("Detailed response info not found", record)
}
})

t.Run("Detailed analytics with cache", func(t *testing.T) {
defer resetTestConfig()
globalConf := config.Global()
globalConf.AnalyticsConfig.EnableDetailedRecording = true
config.SetGlobal(globalConf)

buildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = false
spec.Proxy.ListenPath = "/"
spec.CacheOptions = apidef.CacheOptions{
CacheTimeout: 120,
EnableCache: true,
CacheAllSafeRequests: true,
}
})

key := createSession()

authHeaders := map[string]string{
"authorization": key,
}

ts.Run(t, []test.TestCase{
{Path: "/", Headers: authHeaders, Code: 200},
{Path: "/", Headers: authHeaders, Code: 200},
}...)

// let records to to be sent
time.Sleep(recordsBufferFlushInterval + 50)

results := analytics.Store.GetAndDeleteSet(analyticsKeyName)
if len(results) != 2 {
t.Error("Should return 1 record: ", len(results))
}

// Take second cached request
var record AnalyticsRecord
msgpack.Unmarshal(results[1].([]byte), &record)
if record.ResponseCode != 200 {
t.Error("Analytics record do not match", record)
}

if record.RawRequest == "" {
t.Error("Detailed request info not found", record)
}

if record.RawResponse == "" {
t.Error("Detailed response info not found", record)
}
})
}

func TestListener(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion mw_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
if err != nil {
log.Error("Could not create response object: ", err)
}
nopCloseResponseBody(newRes)

defer newRes.Body.Close()
for _, h := range hopHeaders {
Expand Down Expand Up @@ -263,7 +264,7 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req

// Record analytics
if !m.Spec.DoNotTrack {
go m.sh.RecordHit(r, 0, newRes.StatusCode, nil)
go m.sh.RecordHit(r, 0, newRes.StatusCode, newRes)
}

// Stop any further execution
Expand Down
11 changes: 6 additions & 5 deletions reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) *htt
}

func (p *ReverseProxy) ServeHTTPForCache(rw http.ResponseWriter, req *http.Request) *http.Response {
return p.WrappedServeHTTP(rw, req, true)
resp := p.WrappedServeHTTP(rw, req, true)

nopCloseResponseBody(resp)

return resp
}

func (p *ReverseProxy) CheckHardTimeoutEnforced(spec *APISpec, req *http.Request) (bool, int) {
Expand Down Expand Up @@ -890,11 +894,8 @@ func (n nopCloser) Read(p []byte) (int, error) {
return num, err
}

// Close is a no-op Close plus moves position to the start just in case
// Close is a no-op Close
func (n nopCloser) Close() error {
// seek to the start if body ever called to be closed
n.Seek(0, io.SeekStart)

return nil
}

Expand Down

0 comments on commit d820690

Please sign in to comment.