Browse Source

add http assert

Paul 6 years ago
parent
commit
9b977d3640
1 changed files with 37 additions and 0 deletions
  1. 37
    0
      request/assert.go

+ 37
- 0
request/assert.go View File

@@ -0,0 +1,37 @@
1
+package request
2
+
3
+import (
4
+	"context"
5
+	"io"
6
+	"net/http"
7
+	"net/http/httptest"
8
+)
9
+
10
+// HttpRequestConfig http request config
11
+type HttpRequestConfig struct {
12
+	Method  string
13
+	URL     string
14
+	Param   io.Reader
15
+	Header  map[string]string
16
+	Context context.Context
17
+}
18
+
19
+// AssertHttpRequest check http request
20
+func AssertHttpRequest(handler http.Handler, config HttpRequestConfig) *httptest.ResponseRecorder {
21
+	r := httptest.NewRequest(config.Method, config.URL, config.Param)
22
+	w := httptest.NewRecorder()
23
+
24
+	if config.Header != nil {
25
+		for k, v := range config.Header {
26
+			r.Header.Set(k, v)
27
+		}
28
+	}
29
+
30
+	if config.Context != nil {
31
+		r.WithContext(config.Context)
32
+	}
33
+
34
+	handler.ServeHTTP(w, r)
35
+
36
+	return w
37
+}