@@ -8,24 +8,30 @@ import (
8
8
"net/http"
9
9
"strconv"
10
10
"testing"
11
+ "time"
11
12
12
13
"github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
14
- "github.com/weaveworks/common/httpgrpc"
15
15
"github.com/weaveworks/common/user"
16
16
17
17
"github.com/cortexproject/cortex/pkg/querier/tripperware"
18
18
)
19
19
20
20
func TestRequest (t * testing.T ) {
21
+ now := time .Now ()
22
+ codec := instantQueryCodec {now : func () time.Time {
23
+ return now
24
+ }}
21
25
22
26
for _ , tc := range []struct {
23
27
url string
28
+ expectedUrl string
24
29
expected tripperware.Request
25
30
expectedErr error
26
31
}{
27
32
{
28
- url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680" ,
33
+ url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680" ,
34
+ expectedUrl : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680" ,
29
35
expected : & PrometheusRequest {
30
36
Path : "/api/v1/query" ,
31
37
Time : 1536673680 * 1e3 ,
@@ -37,7 +43,8 @@ func TestRequest(t *testing.T) {
37
43
},
38
44
},
39
45
{
40
- url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680" ,
46
+ url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680" ,
47
+ expectedUrl : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680" ,
41
48
expected : & PrometheusRequest {
42
49
Path : "/api/v1/query" ,
43
50
Time : 1536673680 * 1e3 ,
@@ -49,8 +56,17 @@ func TestRequest(t *testing.T) {
49
56
},
50
57
},
51
58
{
52
- url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all" ,
53
- expectedErr : httpgrpc .Errorf (http .StatusBadRequest , "invalid parameter \" time\" ; cannot parse \" \" to a valid timestamp" ),
59
+ url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29" ,
60
+ expectedUrl : fmt .Sprintf ("%s%d" , "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=" , now .Unix ()),
61
+ expected : & PrometheusRequest {
62
+ Path : "/api/v1/query" ,
63
+ Time : now .Unix () * 1e3 ,
64
+ Query : "sum(container_memory_rss) by (namespace)" ,
65
+ Stats : "" ,
66
+ Headers : map [string ][]string {
67
+ "Test-Header" : {"test" },
68
+ },
69
+ },
54
70
},
55
71
} {
56
72
t .Run (tc .url , func (t * testing.T ) {
@@ -63,16 +79,16 @@ func TestRequest(t *testing.T) {
63
79
// Get a deep copy of the request with Context changed to ctx
64
80
r = r .Clone (ctx )
65
81
66
- req , err := InstantQueryCodec .DecodeRequest (ctx , r , []string {"Test-Header" })
82
+ req , err := codec .DecodeRequest (ctx , r , []string {"Test-Header" })
67
83
if err != nil {
68
84
require .EqualValues (t , tc .expectedErr , err )
69
85
return
70
86
}
71
87
require .EqualValues (t , tc .expected , req )
72
88
73
- rdash , err := InstantQueryCodec .EncodeRequest (context .Background (), req )
89
+ rdash , err := codec .EncodeRequest (context .Background (), req )
74
90
require .NoError (t , err )
75
- require .EqualValues (t , tc .url , rdash .RequestURI )
91
+ require .EqualValues (t , tc .expectedUrl , rdash .RequestURI )
76
92
})
77
93
}
78
94
}
0 commit comments