@@ -3,127 +3,90 @@ package main_test
33import (
44 "bytes"
55 "context"
6- "crypto/tls"
76 "fmt"
87 "io"
9- "net"
108 "net/http"
9+ "net/http/httptest"
10+ "net/http/httputil"
1111 "testing"
12- "time"
1312
1413 "github.com/go-courier/logr"
1514 "github.com/go-courier/logr/slog"
1615 "github.com/octohelm/courier/example/apis"
1716 "github.com/octohelm/courier/example/client/example"
1817 domainorg "github.com/octohelm/courier/example/pkg/domain/org"
1918 "github.com/octohelm/courier/internal/testingutil"
20- "github.com/octohelm/courier/pkg/courierhttp/client"
2119 "github.com/octohelm/courier/pkg/courierhttp/handler/httprouter"
2220 testingx "github.com/octohelm/x/testing"
23- "golang.org/x/net/http2 "
21+ "github.com/octohelm/x/testing/bdd "
2422)
2523
26- var htLogger = client .HttpTransportFunc (func (req * http.Request , next client.RoundTrip ) (* http.Response , error ) {
27- startedAt := time .Now ()
28-
29- ctx , logger := logr .Start (req .Context (), "Request" )
30- defer logger .End ()
31-
32- resp , err := next (req .WithContext (ctx ))
33-
34- defer func () {
35- cost := time .Since (startedAt )
36-
37- logger := logger .WithValues (
38- "cost" , fmt .Sprintf ("%0.3fms" , float64 (cost / time .Millisecond )),
39- "method" , req .Method ,
40- "url" , req .URL .String (),
41- "metadata" , req .Header ,
42- "http.content-length" , req .ContentLength ,
43- )
44-
45- if err == nil {
46- logger .WithValues ("response.proto" , resp .Proto ).Info ("success" )
47- } else {
48- logger .Warn (fmt .Errorf ("http request failed: %w" , err ))
49- }
50- }()
51-
52- return resp , err
53- })
54-
5524func TestAll (t * testing.T ) {
56- h , err := httprouter .New (apis .R , "example" )
57- testingx .Expect (t , err , testingx .BeNil [error ]())
58-
59- srv := testingutil .Serve (t , h )
60-
61- c := & example.Client {
62- Endpoint : srv .URL ,
63- HttpTransports : []client.HttpTransport {htLogger },
64- }
65- ctx := c .InjectContext (context .Background ())
66- ctx = logr .WithLogger (ctx , slog .Logger (slog .Default ()))
67-
68- t .Run ("Do Some Request" , func (t * testing.T ) {
69- org := & example.GetOrg {}
70- org .OrgName = "test"
71-
72- resp , err := example .Do (ctx , org )
73- testingx .Expect (t , err , testingx .BeNil [error ]())
74- testingx .Expect (t , resp .Name , testingx .Be (org .OrgName ))
75- testingx .Expect (t , resp .Type , testingx .Be (domainorg .TYPE__GOV ))
76- })
77-
78- t .Run ("Do Some Request with h2" , func (t * testing.T ) {
79- org := & example.GetOrg {}
80- org .OrgName = "test"
81-
82- resp , err := example .Do (client .ContextWithRoundTripperCreator (ctx , func () http.RoundTripper {
83- return & http2.Transport {
84- AllowHTTP : true ,
85- DialTLSContext : func (ctx context.Context , network , addr string , cfg * tls.Config ) (net.Conn , error ) {
86- return net .Dial (network , addr )
87- },
88- }
89- }), org )
90- testingx .Expect (t , err , testingx .BeNil [error ]())
91- testingx .Expect (t , resp .Name , testingx .Be (org .OrgName ))
92- })
93-
94- t .Run ("Upload" , func (t * testing.T ) {
95- v := & example.UploadBlob {}
96- v .RequestBody = io .NopCloser (bytes .NewBufferString ("1234567" ))
97-
98- _ , err := example .Do (ctx , v )
99- testingx .Expect (t , err , testingx .BeNil [error ]())
100- })
25+ h := bdd .Must (httprouter .New (apis .R , "example" ))
10126
102- t .Run ("UploadStoreBlob" , func (t * testing.T ) {
103- v := & example.UploadStoreBlob {}
104- v .Scope = "a/b/c"
105- v .RequestBody = io .NopCloser (bytes .NewBufferString ("1234567" ))
27+ hh := http .HandlerFunc (func (writer http.ResponseWriter , request * http.Request ) {
28+ raw , _ := httputil .DumpRequest (request , true )
29+ fmt .Println (string (raw ))
10630
107- _ , err := example .Do (ctx , v )
108- testingx .Expect (t , err , testingx .BeNil [error ]())
31+ h .ServeHTTP (writer , request )
10932 })
11033
111- t .Run ("GetStoreBlob" , func (t * testing.T ) {
112- v := & example.GetStoreBlob {}
113- v .Scope = "a/b/c"
114- v .Digest = "xxx"
115-
116- resp , err := example .Do (ctx , v )
117- testingx .Expect (t , err , testingx .BeNil [error ]())
118- testingx .Expect (t , * resp , testingx .Be ("a/b/c@xxx" ))
119- })
120-
121- t .Run ("GetFile" , func (t * testing.T ) {
122- v := & example.GetFile {}
123- v .Path = "a/b/c"
124-
125- resp , err := example .Do (ctx , v )
126- testingx .Expect (t , err , testingx .BeNil [error ]())
127- testingx .Expect (t , * resp , testingx .Be ("a/b/c" ))
128- })
34+ for i , srv := range []* httptest.Server {
35+ testingutil .ServeWithH2C (t , hh ),
36+ testingutil .Serve (t , hh ),
37+ } {
38+ t .Run (fmt .Sprintf ("serve http/%d" , 2 - i ), func (t * testing.T ) {
39+ c := & example.Client {Endpoint : srv .URL }
40+
41+ ctx := c .InjectContext (context .Background ())
42+ ctx = logr .WithLogger (ctx , slog .Logger (slog .Default ()))
43+
44+ t .Run ("Do Some Request" , func (t * testing.T ) {
45+ org := & example.GetOrg {}
46+ org .OrgName = "test"
47+
48+ resp , err := example .Do (ctx , org )
49+ testingx .Expect (t , err , testingx .BeNil [error ]())
50+
51+ testingx .Expect (t , resp .Name , testingx .Be (org .OrgName ))
52+ testingx .Expect (t , resp .Type , testingx .Be (domainorg .TYPE__GOV ))
53+ })
54+
55+ t .Run ("Upload" , func (t * testing.T ) {
56+ v := & example.UploadBlob {}
57+ v .RequestBody = io .NopCloser (bytes .NewBufferString ("1234567" ))
58+
59+ _ , err := example .Do (ctx , v )
60+ testingx .Expect (t , err , testingx .BeNil [error ]())
61+ })
62+
63+ t .Run ("UploadStoreBlob" , func (t * testing.T ) {
64+ v := & example.UploadStoreBlob {}
65+ v .Scope = "a/b/c"
66+ v .RequestBody = io .NopCloser (bytes .NewBufferString ("1234567" ))
67+
68+ _ , err := example .Do (ctx , v )
69+ testingx .Expect (t , err , testingx .BeNil [error ]())
70+ })
71+
72+ t .Run ("GetStoreBlob" , func (t * testing.T ) {
73+ v := & example.GetStoreBlob {}
74+ v .Scope = "a/b/c"
75+ v .Digest = "xxx"
76+
77+ resp , err := example .Do (ctx , v )
78+ testingx .Expect (t , err , testingx .BeNil [error ]())
79+ testingx .Expect (t , * resp , testingx .Be ("a/b/c@xxx" ))
80+ })
81+
82+ t .Run ("GetFile" , func (t * testing.T ) {
83+ v := & example.GetFile {}
84+ v .Path = "a/b/c"
85+
86+ resp , err := example .Do (ctx , v )
87+ testingx .Expect (t , err , testingx .BeNil [error ]())
88+ testingx .Expect (t , * resp , testingx .Be ("a/b/c" ))
89+ })
90+ })
91+ }
12992}
0 commit comments