55 "context"
66 "fmt"
77 "net/http"
8- "strconv"
98 "testing"
109 "time"
1110
@@ -23,12 +22,7 @@ import (
2322)
2423
2524func TestHTTPServerUsingRedis (t * testing.T ) {
26- httpPort := testutil .GetFreePort (t )
27- t .Setenv ("HTTP_PORT" , strconv .Itoa (httpPort ))
28- host := fmt .Sprint ("http://localhost:" , httpPort )
29-
30- port := testutil .GetFreePort (t )
31- t .Setenv ("METRICS_PORT" , strconv .Itoa (port ))
25+ configs := testutil .NewServerConfigs (t )
3226
3327 go main ()
3428 time .Sleep (100 * time .Millisecond ) // Giving some time to start the server
@@ -51,7 +45,7 @@ func TestHTTPServerUsingRedis(t *testing.T) {
5145 }
5246
5347 for i , tc := range tests {
54- req , _ := http .NewRequest (tc .method , host + tc .path , bytes .NewBuffer (tc .body ))
48+ req , _ := http .NewRequest (tc .method , configs . HTTPHost + tc .path , bytes .NewBuffer (tc .body ))
5549 req .Header .Set ("content-type" , "application/json" )
5650 c := http.Client {}
5751 resp , err := c .Do (req )
@@ -63,11 +57,7 @@ func TestHTTPServerUsingRedis(t *testing.T) {
6357}
6458
6559func TestRedisSetHandler (t * testing.T ) {
66- metricsPort := testutil .GetFreePort (t )
67- httpPort := testutil .GetFreePort (t )
68-
69- t .Setenv ("METRICS_PORT" , strconv .Itoa (metricsPort ))
70- t .Setenv ("HTTP_PORT" , strconv .Itoa (httpPort ))
60+ configs := testutil .NewServerConfigs (t )
7161
7262 a := gofr .New ()
7363 logger := logging .NewLogger (logging .DEBUG )
@@ -78,7 +68,7 @@ func TestRedisSetHandler(t *testing.T) {
7868
7969 mock .ExpectSet ("key" , "value" , 5 * time .Minute ).SetErr (testutil.CustomError {ErrorMessage : "redis get error" })
8070
81- req , _ := http .NewRequestWithContext (context .Background (), http .MethodGet , fmt .Sprintf ("http://localhost:%d/handle" , httpPort ), bytes .NewBuffer ([]byte (`{"key":"value"}` )))
71+ req , _ := http .NewRequestWithContext (context .Background (), http .MethodGet , fmt .Sprintf ("http://localhost:%d/handle" , configs . HTTPPort ), bytes .NewBuffer ([]byte (`{"key":"value"}` )))
8272 req .Header .Set ("content-type" , "application/json" )
8373 gofrReq := gofrHTTP .NewRequest (req )
8474
@@ -92,11 +82,7 @@ func TestRedisSetHandler(t *testing.T) {
9282}
9383
9484func TestRedisPipelineHandler (t * testing.T ) {
95- metricsPort := testutil .GetFreePort (t )
96- httpPort := testutil .GetFreePort (t )
97-
98- t .Setenv ("METRICS_PORT" , strconv .Itoa (metricsPort ))
99- t .Setenv ("HTTP_PORT" , strconv .Itoa (httpPort ))
85+ configs := testutil .NewServerConfigs (t )
10086
10187 a := gofr .New ()
10288 logger := logging .NewLogger (logging .DEBUG )
@@ -108,7 +94,7 @@ func TestRedisPipelineHandler(t *testing.T) {
10894 mock .ExpectSet ("testKey1" , "testValue1" , time .Minute * 5 ).SetErr (testutil.CustomError {ErrorMessage : "redis get error" })
10995 mock .ClearExpect ()
11096
111- req , _ := http .NewRequestWithContext (context .Background (), http .MethodGet , fmt .Sprint ("http://localhost:" , httpPort , "/handle" ), bytes .NewBuffer ([]byte (`{"key":"value"}` )))
97+ req , _ := http .NewRequestWithContext (context .Background (), http .MethodGet , fmt .Sprint ("http://localhost:" , configs . HTTPHost , "/handle" ), bytes .NewBuffer ([]byte (`{"key":"value"}` )))
11298 req .Header .Set ("content-type" , "application/json" )
11399
114100 gofrReq := gofrHTTP .NewRequest (req )
0 commit comments