@@ -20,11 +20,12 @@ const (
2020)
2121
2222type mockServerExpectedInput struct {
23- method string
24- path string
25- query string
26- contentType string
27- body string
23+ method string
24+ path string
25+ query string
26+ authorization string
27+ contentType string
28+ body string
2829}
2930
3031type mockServerOutput struct {
@@ -33,6 +34,10 @@ type mockServerOutput struct {
3334}
3435
3536func newMockServer (input mockServerExpectedInput , output mockServerOutput ) * httptest.Server {
37+ if input .authorization == "" {
38+ input .authorization = authorizationHeader
39+ }
40+
3641 return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
3742 if r .Method != input .method {
3843 w .WriteHeader (http .StatusMethodNotAllowed )
@@ -49,7 +54,7 @@ func newMockServer(input mockServerExpectedInput, output mockServerOutput) *http
4954 return
5055 }
5156
52- if r .Header .Get ("Authorization" ) != authorizationHeader {
57+ if r .Header .Get ("Authorization" ) != input . authorization {
5358 w .WriteHeader (http .StatusUnauthorized )
5459 return
5560 }
@@ -80,6 +85,43 @@ func newClient(baseURL string) *smsgateway.Client {
8085 })
8186}
8287
88+ func newJWTClient (baseURL string ) * smsgateway.Client {
89+ return smsgateway .NewClient (smsgateway.Config {
90+ BaseURL : baseURL ,
91+ Token : password ,
92+ })
93+ }
94+
95+ func TestJWTClient_Send (t * testing.T ) {
96+ server := newMockServer (mockServerExpectedInput {
97+ method : http .MethodPost ,
98+ path : "/messages" ,
99+ authorization : "Bearer " + password ,
100+ contentType : "application/json" ,
101+ body : `{"textMessage":{"text":"Hello World!"},"phoneNumbers":["+1234567890"]}` ,
102+ }, mockServerOutput {
103+ code : http .StatusCreated ,
104+ body : `{}` ,
105+ })
106+ defer server .Close ()
107+
108+ client := newJWTClient (server .URL )
109+
110+ t .Run ("Success" , func (t * testing.T ) {
111+ message := smsgateway.Message {
112+ TextMessage : & smsgateway.TextMessage {
113+ Text : "Hello World!" ,
114+ },
115+ PhoneNumbers : []string {"+1234567890" },
116+ }
117+
118+ _ , err := client .Send (context .Background (), message )
119+ if err != nil {
120+ t .Errorf ("Send() error = %v" , err )
121+ }
122+ })
123+ }
124+
83125func TestClient_Send (t * testing.T ) {
84126 type args struct {
85127 ctx context.Context
@@ -499,6 +541,14 @@ func TestClient_DeleteDevice(t *testing.T) {
499541 },
500542 wantErr : false ,
501543 },
544+ {
545+ name : "Not Found" ,
546+ c : client ,
547+ args : args {
548+ deviceID : "456" ,
549+ },
550+ wantErr : true ,
551+ },
502552 }
503553
504554 for _ , tt := range tests {
@@ -554,28 +604,6 @@ func TestClient_CheckHealth(t *testing.T) {
554604 }
555605 })
556606 }
557-
558- for _ , tt := range tests {
559- t .Run (tt .name , func (t * testing.T ) {
560- server := newMockServer (mockServerExpectedInput {
561- method : http .MethodGet ,
562- path : "/health" ,
563- }, mockServerOutput {
564- code : tt .code ,
565- body : tt .body ,
566- })
567- defer server .Close ()
568-
569- client := newClient (server .URL )
570- resp , err := client .CheckHealth (context .Background ())
571- if (err != nil ) != tt .wantErr {
572- t .Errorf ("CheckHealth error = %v, wantErr %v" , err , tt .wantErr )
573- }
574- if ! tt .wantErr && ! reflect .DeepEqual (resp , tt .want ) {
575- t .Errorf ("CheckHealth response = %v, want %v" , resp , tt .want )
576- }
577- })
578- }
579607}
580608
581609func TestClient_ExportInbox (t * testing.T ) {
@@ -876,12 +904,12 @@ func TestClient_GenerateToken(t *testing.T) {
876904 name : "Success" ,
877905 req : smsgateway.TokenRequest {Scopes : []string {"messages:read" }, TTL : 3600 },
878906 code : http .StatusOK ,
879- body : `{"id":"UWSpnfJzsdQ81JSU2Q ","token_type":"Bearer","access_token":"vLNTuO17eG7kp0XS0 ","expires_at":"2021 -01-01T00:00:00Z"}` ,
907+ body : `{"id":"token_id_example ","token_type":"Bearer","access_token":"access_token_example ","expires_at":"2025 -01-01T00:00:00Z"}` ,
880908 want : smsgateway.TokenResponse {
881- ID : "UWSpnfJzsdQ81JSU2Q " ,
909+ ID : "token_id_example " ,
882910 TokenType : "Bearer" ,
883- AccessToken : "vLNTuO17eG7kp0XS0 " ,
884- ExpiresAt : time .Date (2021 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
911+ AccessToken : "access_token_example " ,
912+ ExpiresAt : time .Date (2025 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
885913 },
886914 wantErr : false ,
887915 },
0 commit comments