github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/account_link_test.go (about)

     1  // Copyright 2016 LINE Corporation
     2  //
     3  // LINE Corporation licenses this file to you under the Apache License,
     4  // version 2.0 (the "License"); you may not use this file except in compliance
     5  // with the License. You may obtain a copy of the License at:
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package linebot
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"reflect"
    23  	"strconv"
    24  	"strings"
    25  	"testing"
    26  )
    27  
    28  func TestIssueLinkToken(t *testing.T) {
    29  	type want struct {
    30  		RequestBody []byte
    31  		Response    *LinkTokenResponse
    32  		Error       error
    33  	}
    34  	testCases := []struct {
    35  		Label        string
    36  		UserID       string
    37  		Response     []byte
    38  		ResponseCode int
    39  		Want         want
    40  	}{
    41  		{
    42  			Label:        "Success",
    43  			UserID:       "u206d25c2ea6bd87c17655609a1c37cb8",
    44  			ResponseCode: 200,
    45  			Response:     []byte(`{"linkToken":"NMZTNuVrPTqlr2IF8Bnymkb7rXfYv5EY"}`),
    46  			Want: want{
    47  				RequestBody: []byte(""),
    48  				Response:    &LinkTokenResponse{LinkToken: "NMZTNuVrPTqlr2IF8Bnymkb7rXfYv5EY"},
    49  			},
    50  		},
    51  		{
    52  			Label:        "Empty UserID",
    53  			UserID:       "",
    54  			ResponseCode: 400,
    55  			Response:     nil,
    56  			Want: want{
    57  				RequestBody: []byte(""),
    58  				Error:       &APIError{Code: 400, Response: nil},
    59  			},
    60  		},
    61  	}
    62  
    63  	var currentTestIdx int
    64  	server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    65  		defer r.Body.Close()
    66  		tc := testCases[currentTestIdx]
    67  		if r.Method != http.MethodPost {
    68  			t.Errorf("Method %s; want %s", r.Method, http.MethodPost)
    69  		}
    70  		// if path variable is empty, "//" will be normalized in request
    71  		endpoint := strings.Replace(fmt.Sprintf(APIEndpointLinkToken, tc.UserID), "//", "/", -1)
    72  		if r.URL.Path != endpoint {
    73  			t.Errorf("URLPath %s; want %s", r.URL.Path, endpoint)
    74  		}
    75  		body, err := io.ReadAll(r.Body)
    76  		if err != nil {
    77  			t.Fatal(err)
    78  		}
    79  		if !reflect.DeepEqual(body, tc.Want.RequestBody) {
    80  			t.Errorf("RequestBody\n %s; want\n %s", body, tc.Want.RequestBody)
    81  		}
    82  		w.WriteHeader(tc.ResponseCode)
    83  		w.Write(tc.Response)
    84  	}))
    85  	defer server.Close()
    86  
    87  	dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    88  		defer r.Body.Close()
    89  		t.Error("Unexpected data API call")
    90  		w.WriteHeader(404)
    91  		w.Write([]byte(`{"message":"Not found"}`))
    92  	}))
    93  	defer dataServer.Close()
    94  
    95  	client, err := mockClient(server, dataServer)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	for i, tc := range testCases {
   100  		currentTestIdx = i
   101  		t.Run(strconv.Itoa(i)+"/"+tc.Label, func(t *testing.T) {
   102  			res, err := client.IssueLinkToken(tc.UserID).Do()
   103  			if tc.Want.Error != nil {
   104  				if !reflect.DeepEqual(err, tc.Want.Error) {
   105  					t.Errorf("Error %v; want  %v", err, tc.Want.Error)
   106  				}
   107  			} else {
   108  				if err != nil {
   109  					t.Error(err)
   110  				}
   111  			}
   112  			if tc.Want.Response != nil {
   113  				if !reflect.DeepEqual(res, tc.Want.Response) {
   114  					t.Errorf("Response %v; want %v", res, tc.Want.Response)
   115  				}
   116  			}
   117  		})
   118  	}
   119  }