MCPcopy Create free account
hub / github.com/1buran/rHttp / TestSession

Function TestSession

session_test.go:31–141  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestSession(t *testing.T) {
32 var rq *http.Request
33 var rs *http.Response
34
35 rqf := map[string][]string{}
36 rsb := []string{}
37
38 t.Run("create new session with empty request and response", func(t *testing.T) {
39 _, err := NewSession(rq, rs, 10, "10.57µs", rqf, rsb)
40 if err != nil {
41 t.Fatalf("cannot create new session, error: %s", err)
42 }
43 })
44
45 rq = &http.Request{
46 Method: "GET",
47 URL: &url.URL{
48 Host: "localhost",
49 Path: "/api/endpoint",
50 RawQuery: "order_by=name&limit=10",
51 },
52 Header: make(http.Header),
53 }
54 rq.Header.Set("content-type", "application/json")
55 rq.Header.Set("auth-token", "token")
56
57 rs = &http.Response{
58 Status: "200 OK",
59 Proto: "HTTP/1.1",
60 Header: make(http.Header),
61 }
62 rs.Header.Set("connection", "keep-alive")
63 rs.Header.Set("content-length", "34")
64 rs.Header.Set("content-type", "application/json; charset=utf-8")
65 rs.Header.Set("date", "Date: Wed, 20 Mar 2024 09:11:23 GMT")
66
67 rsb = []string{"{", `"msg": "hello!",`, `"id": 455`, "}"}
68
69 t.Run("create new session filled request and response", func(t *testing.T) {
70 s, err := NewSession(rq, rs, 10, "10.57µs", rqf, rsb)
71 if err != nil {
72 t.Fatalf("cannot create new session, error: %s", err)
73 }
74 if s.ReqCount != 10 {
75 t.Errorf("expected req count: 10, got: %d", s.ReqCount)
76 }
77 if s.Response.Status != "200 OK" {
78 t.Errorf("expected res status: 200 OK, got: %s", s.Response.Status)
79 }
80 v, ok := s.Response.Headers["Content-Type"]
81 if !ok || slices.Compare(v, []string{"application/json; charset=utf-8"}) != 0 {
82 t.Errorf(
83 `expected: res header Content-Type="application/json; charset=utf-8", got: %#v`, v,
84 )
85 }
86 if slices.Compare(s.Response.BodyLines, rsb) != 0 {
87 t.Errorf("expected res body lines %s != %s", rsb, s.Response.BodyLines)
88 }

Callers

nothing calls this directly

Calls 4

SaveMethod · 0.95
ContainsMethod · 0.95
LoadMethod · 0.95
NewSessionFunction · 0.85

Tested by

no test coverage detected