@@ -3,18 +3,20 @@ package main
33import (
44 "encoding/json"
55 "fmt"
6- "io"
7- "net/http"
86 "strconv"
97 "testing"
108
9+ "github.com/go-resty/resty/v2"
1110 "github.com/stretchr/testify/assert"
1211)
1312
1413func TestInfo (t * testing.T ) {
14+ t .Parallel ()
15+
1516 type Expected struct {
16- res map [string ]interface {}
17- err error
17+ body map [string ]interface {}
18+ status int
19+ err error
1820 }
1921
2022 cases := []struct {
@@ -25,40 +27,44 @@ func TestInfo(t *testing.T) {
2527 {
2628 description : "case 1" ,
2729 compose : New ().
30+ WithEnv ("SHELLHUB_API_PORT" , strconv .Itoa (getFreePort (t ))).
2831 WithEnv ("SHELLHUB_HTTP_PORT" , strconv .Itoa (getFreePort (t ))).
2932 WithEnv ("SHELLHUB_SSH_PORT" , strconv .Itoa (getFreePort (t ))).
30- WithEnv ("SHELLHUB_VERSION" , "v0.14 .2" ).
33+ WithEnv ("SHELLHUB_VERSION" , "v0.25 .2" ).
3134 Build (),
3235 expected : func (httpPort , sshPort string ) Expected {
3336 return Expected {
34- res : map [string ]interface {}{
37+ body : map [string ]interface {}{
3538 "endpoints" : map [string ]interface {}{
3639 "api" : fmt .Sprintf ("localhost:%s" , httpPort ),
3740 "ssh" : fmt .Sprintf ("localhost:%s" , sshPort ),
3841 },
39- "version" : "v0.14 .2" ,
42+ "version" : "v0.25 .2" ,
4043 },
41- err : nil ,
44+ status : 200 ,
45+ err : nil ,
4246 }
4347 },
4448 },
4549 {
4650 description : "case 2" ,
4751 compose : New ().
52+ WithEnv ("SHELLHUB_API_PORT" , strconv .Itoa (getFreePort (t ))).
4853 WithEnv ("SHELLHUB_HTTP_PORT" , strconv .Itoa (getFreePort (t ))).
4954 WithEnv ("SHELLHUB_SSH_PORT" , strconv .Itoa (getFreePort (t ))).
5055 WithEnv ("SHELLHUB_VERSION" , "v0.10.2" ).
5156 Build (),
5257 expected : func (httpPort , sshPort string ) Expected {
5358 return Expected {
54- res : map [string ]interface {}{
59+ body : map [string ]interface {}{
5560 "endpoints" : map [string ]interface {}{
5661 "api" : fmt .Sprintf ("localhost:%s" , httpPort ),
5762 "ssh" : fmt .Sprintf ("localhost:%s" , sshPort ),
5863 },
5964 "version" : "v0.10.2" ,
6065 },
61- err : nil ,
66+ status : 200 ,
67+ err : nil ,
6268 }
6369 },
6470 },
@@ -68,36 +74,22 @@ func TestInfo(t *testing.T) {
6874 // Avoid "loop variable <> captured by func literal"
6975 tc := tt
7076
71- t . Run (tc .description , func (t * testing.T ) {
77+ tc . compose . Run (t , tc .description , func (t * testing.T ) {
7278 t .Parallel ()
7379
74- tc .compose .Run (t , func () {
75- fmt .Printf ("desciption %s\n " , tc .description )
76-
77- fmt .Printf ("%s request to %s\n " , tc .description , fmt .Sprintf ("http://localhost:%s/info" , tc .compose .GetEnv ("SHELLHUB_HTTP_PORT" )))
78-
79- // res, _ := resty.
80- // New().
81- // R().
82- // Get(fmt.Sprintf("http://localhost:%s/info", tc.compose.GetEnv("SHELLHUB_HTTP_PORT")))
83-
84- res , err := http .Get (fmt .Sprintf ("http://localhost:%s/info" , tc .compose .GetEnv ("SHELLHUB_HTTP_PORT" )))
85- assert .NoError (t , err )
86- defer res .Body .Close ()
87-
88- foo , err := io .ReadAll (res .Body )
89- assert .NoError (t , err )
80+ res , err := resty .
81+ New ().
82+ R ().
83+ Get (fmt .Sprintf ("http://localhost:%s/info" , tc .compose .GetEnv ("SHELLHUB_HTTP_PORT" )))
9084
91- body := map [string ]interface {}{}
92- assert .NoError (t , json .Unmarshal (foo , & body ))
85+ body := map [string ]interface {}{}
86+ assert .NoError (t , json .Unmarshal (res . Body () , & body ))
9387
94- fmt .Printf ("%s http: %+v\n " , tc .description , body )
95- fmt .Printf ("%s env: %s %s %s\n " , tc .description , tc .compose .GetEnv ("SHELLHUB_HTTP_PORT" ), tc .compose .GetEnv ("SHELLHUB_SSH_PORT" ), tc .compose .GetEnv ("SHELLHUB_VERSION" ))
96- fmt .Printf ("%s expected: %+v\n " , tc .description , tc .expected (tc .compose .GetEnv ("SHELLHUB_HTTP_PORT" ), tc .compose .GetEnv ("SHELLHUB_SSH_PORT" )).res )
97- fmt .Println ("********************************************" )
98- //
99- // assert.Equal(t, tc.expected(tc.compose.GetEnv("SHELLHUB_HTTP_PORT"), tc.compose.GetEnv("SHELLHUB_SSH_PORT")), Expected{body, err})
100- })
88+ assert .Equal (
89+ t ,
90+ tc .expected (tc .compose .GetEnv ("SHELLHUB_HTTP_PORT" ), tc .compose .GetEnv ("SHELLHUB_SSH_PORT" )),
91+ Expected {body , res .StatusCode (), err },
92+ )
10193 })
10294 }
10395}
0 commit comments