github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/client/client_test.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/http"
     7  	"net/url"
     8  	"testing"
     9  
    10  	"github.com/cozy/cozy-stack/client/auth"
    11  	"github.com/cozy/cozy-stack/client/request"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  type testAssertReq func(*http.Request)
    16  type testTransport struct {
    17  	assertFn testAssertReq
    18  }
    19  
    20  func (t *testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    21  	t.assertFn(req)
    22  	return nil, errors.New("ok")
    23  }
    24  
    25  func testClient(assertFn testAssertReq) *http.Client {
    26  	return &http.Client{Transport: &testTransport{assertFn}}
    27  }
    28  
    29  func TestClientWithOAuth(t *testing.T) {
    30  	c := &Client{
    31  		Domain: "foobar",
    32  		Scheme: "https",
    33  		Client: testClient(func(req *http.Request) {
    34  			header := req.Header
    35  			assert.Equal(t, "", header.Get("Authorization"))
    36  			assert.Equal(t, "application/json", header.Get("Content-Type"))
    37  			assert.Equal(t, "application/json", header.Get("Accept"))
    38  			assert.Equal(t, &url.URL{
    39  				Scheme: "https",
    40  				Host:   "foobar",
    41  				Path:   "/auth/register",
    42  			}, req.URL)
    43  			var v auth.Client
    44  			err := json.NewDecoder(req.Body).Decode(&v)
    45  			assert.NoError(t, err)
    46  			assert.EqualValues(t, v, auth.Client{
    47  				RedirectURIs: []string{"http://redirectto/"},
    48  				ClientName:   "name",
    49  				ClientKind:   "kind",
    50  				ClientURI:    "uri",
    51  				SoftwareID:   "github.com/cozy/cozy-stack",
    52  			})
    53  		}),
    54  		AuthClient: &auth.Client{
    55  			RedirectURIs: []string{"http://redirectto/"},
    56  			ClientName:   "name",
    57  			ClientKind:   "kind",
    58  			ClientURI:    "uri",
    59  		},
    60  	}
    61  	_, err := c.Req(&request.Options{
    62  		Method: "PUT",
    63  		Path:   "/p/a/t/h",
    64  	})
    65  	assert.Error(t, err)
    66  }
    67  
    68  func TestClientWithoutOAuth(t *testing.T) {
    69  	type testjson struct {
    70  		Key string `json:"key"`
    71  	}
    72  	c := &Client{
    73  		Domain:     "foobar",
    74  		Scheme:     "https",
    75  		UserAgent:  "user/agent",
    76  		Authorizer: &request.BearerAuthorizer{Token: "token"},
    77  		Client: testClient(func(req *http.Request) {
    78  			header := req.Header
    79  			assert.Equal(t, "Bearer token", header.Get("Authorization"))
    80  			assert.Equal(t, "user/agent", header.Get("User-Agent"))
    81  			assert.Equal(t, "application/json", header.Get("Content-Type"))
    82  			assert.Equal(t, "application/json", header.Get("Accept"))
    83  			assert.Equal(t, &url.URL{
    84  				Scheme:   "https",
    85  				Host:     "foobar",
    86  				Path:     "/p/a/t/h",
    87  				RawQuery: "q=value",
    88  			}, req.URL)
    89  			var v testjson
    90  			err := json.NewDecoder(req.Body).Decode(&v)
    91  			assert.NoError(t, err)
    92  			assert.Equal(t, v.Key, "Value")
    93  		}),
    94  	}
    95  
    96  	body, err := request.WriteJSON(&testjson{Key: "Value"})
    97  	assert.NoError(t, err)
    98  
    99  	_, err = c.Req(&request.Options{
   100  		Method:  "PUT",
   101  		Path:    "/p/a/t/h",
   102  		Queries: url.Values{"q": {"value"}},
   103  		Headers: request.Headers{
   104  			"Content-Type": "application/json",
   105  			"Accept":       "application/json",
   106  		},
   107  		Body: body,
   108  	})
   109  	assert.Error(t, err)
   110  }