github.com/prebid/prebid-server/v2@v2.18.0/router/aspects/request_timeout_handler_test.go (about) 1 package aspects 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "strconv" 7 "testing" 8 "time" 9 10 "github.com/julienschmidt/httprouter" 11 "github.com/prebid/prebid-server/v2/config" 12 "github.com/prebid/prebid-server/v2/metrics" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 const reqTimeInQueueHeaderName = "X-Ngx-Request-Time" 18 const reqTimeoutHeaderName = "X-Request-Timeout" 19 20 func TestAny(t *testing.T) { 21 testCases := []struct { 22 reqTimeInQueue string 23 reqTimeOut string 24 setHeaders bool 25 expectedRespCode int 26 expectedRespCodeMessage string 27 expectedRespBody string 28 expectedRespBodyMessage string 29 requestStatusMetrics bool 30 }{ 31 { 32 //TestQueuedRequestTimeoutWithTimeout 33 reqTimeInQueue: "6", 34 reqTimeOut: "5", 35 setHeaders: true, 36 expectedRespCode: http.StatusRequestTimeout, 37 expectedRespCodeMessage: "Http response code is incorrect, should be 408", 38 expectedRespBody: "Queued request processing time exceeded maximum", 39 expectedRespBodyMessage: "Body should have error message", 40 requestStatusMetrics: false, 41 }, 42 { 43 //TestQueuedRequestTimeoutNoTimeout 44 reqTimeInQueue: "0.9", 45 reqTimeOut: "5", 46 setHeaders: true, 47 expectedRespCode: http.StatusOK, 48 expectedRespCodeMessage: "Http response code is incorrect, should be 200", 49 expectedRespBody: "Executed", 50 expectedRespBodyMessage: "Body should be present in response", 51 requestStatusMetrics: true, 52 }, 53 { 54 //TestQueuedRequestNoHeaders 55 reqTimeInQueue: "", 56 reqTimeOut: "", 57 setHeaders: false, 58 expectedRespCode: http.StatusOK, 59 expectedRespCodeMessage: "Http response code is incorrect, should be 200", 60 expectedRespBody: "Executed", 61 expectedRespBodyMessage: "Body should be present in response", 62 requestStatusMetrics: true, 63 }, 64 { 65 //TestQueuedRequestSomeHeaders 66 reqTimeInQueue: "2", 67 reqTimeOut: "", 68 setHeaders: true, 69 expectedRespCode: http.StatusOK, 70 expectedRespCodeMessage: "Http response code is incorrect, should be 200", 71 expectedRespBody: "Executed", 72 expectedRespBodyMessage: "Body should be present in response", 73 requestStatusMetrics: true, 74 }, 75 } 76 77 for _, test := range testCases { 78 reqTimeFloat, _ := strconv.ParseFloat(test.reqTimeInQueue, 64) 79 result := ExecuteAspectRequest(t, test.reqTimeInQueue, test.reqTimeOut, test.setHeaders, metrics.ReqTypeVideo, test.requestStatusMetrics, reqTimeFloat) 80 assert.Equal(t, test.expectedRespCode, result.Code, test.expectedRespCodeMessage) 81 assert.Equal(t, test.expectedRespBody, result.Body.String(), test.expectedRespBodyMessage) 82 } 83 } 84 85 func MockEndpoint() httprouter.Handle { 86 return httprouter.Handle(MockHandler) 87 } 88 89 func MockHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 90 w.Write([]byte("Executed")) 91 } 92 93 func ExecuteAspectRequest(t *testing.T, timeInQueue string, reqTimeout string, setHeaders bool, requestType metrics.RequestType, status bool, requestDuration float64) *httptest.ResponseRecorder { 94 rw := httptest.NewRecorder() 95 req, err := http.NewRequest("POST", "/test", nil) 96 if err != nil { 97 assert.Fail(t, "Unable create mock http request") 98 } 99 if setHeaders { 100 req.Header.Set(reqTimeInQueueHeaderName, timeInQueue) 101 req.Header.Set(reqTimeoutHeaderName, reqTimeout) 102 } 103 104 customHeaders := config.RequestTimeoutHeaders{RequestTimeInQueue: reqTimeInQueueHeaderName, RequestTimeoutInQueue: reqTimeoutHeaderName} 105 106 metrics := &metrics.MetricsEngineMock{} 107 108 metrics.On("RecordRequestQueueTime", status, requestType, time.Duration(requestDuration*float64(time.Second))).Once() 109 110 handler := QueuedRequestTimeout(MockEndpoint(), customHeaders, metrics, requestType) 111 112 r := httprouter.New() 113 r.POST("/test", handler) 114 115 r.ServeHTTP(rw, req) 116 117 return rw 118 }