github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/client_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 "context" 19 "crypto/tls" 20 "fmt" 21 "net/http" 22 "net/http/httptest" 23 "net/url" 24 "reflect" 25 "testing" 26 ) 27 28 func mockClient(server *httptest.Server, dataServer *httptest.Server) (*Client, error) { 29 u, err := url.ParseRequestURI(server.URL) 30 if err != nil { 31 return nil, fmt.Errorf("failed to parse server.URL: %v", err) 32 } 33 du, err := url.ParseRequestURI(dataServer.URL) 34 if err != nil { 35 return nil, fmt.Errorf("failed to parse dataServer.URL: %v", err) 36 } 37 return &Client{ 38 channelSecret: "testsecret", 39 channelToken: "testtoken", 40 endpointBase: u, 41 endpointBaseData: du, 42 httpClient: &http.Client{ 43 Transport: &http.Transport{ 44 TLSClientConfig: &tls.Config{ 45 InsecureSkipVerify: true, 46 }, 47 }, 48 }, 49 }, nil 50 } 51 52 func TestNewClient(t *testing.T) { 53 secret := "testsecret" 54 token := "testtoken" 55 wantURL, _ := url.Parse(APIEndpointBase) 56 wantDataURL, _ := url.Parse(APIEndpointBaseData) 57 client, err := New(secret, token) 58 if err != nil { 59 t.Fatal(err) 60 } 61 if client.channelSecret != secret { 62 t.Errorf("channelSecret %s; want %s", client.channelSecret, secret) 63 } 64 if client.channelToken != token { 65 t.Errorf("channelToken %s; want %s", client.channelToken, token) 66 } 67 if !reflect.DeepEqual(client.endpointBase, wantURL) { 68 t.Errorf("endpointBase %v; want %v", client.endpointBase, wantURL) 69 } 70 if !reflect.DeepEqual(client.endpointBaseData, wantDataURL) { 71 t.Errorf("endpointBase %v; want %v", client.endpointBaseData, wantDataURL) 72 } 73 if client.httpClient != http.DefaultClient { 74 t.Errorf("httpClient %p; want %p", client.httpClient, http.DefaultClient) 75 } 76 } 77 78 func TestNewClientWithOptions(t *testing.T) { 79 secret := "testsecret" 80 token := "testtoken" 81 endpoint := "https://example.test/" 82 dataEndpoint := "https://example-data.test/" 83 httpClient := http.Client{} 84 wantURL, _ := url.Parse(endpoint) 85 wantDataURL, _ := url.Parse(dataEndpoint) 86 client, err := New( 87 secret, 88 token, 89 WithHTTPClient(&httpClient), 90 WithEndpointBase(endpoint), 91 WithEndpointBaseData(dataEndpoint), 92 ) 93 if err != nil { 94 t.Fatal(err) 95 } 96 if !reflect.DeepEqual(client.endpointBase, wantURL) { 97 t.Errorf("endpointBase %v; want %v", client.endpointBase, wantURL) 98 } 99 if !reflect.DeepEqual(client.endpointBaseData, wantDataURL) { 100 t.Errorf("endpointBaseData %v; want %v", client.endpointBaseData, wantDataURL) 101 } 102 if client.httpClient != &httpClient { 103 t.Errorf("httpClient %p; want %p", client.httpClient, &httpClient) 104 } 105 } 106 107 func expectCtxDeadlineExceed(ctx context.Context, err error, t *testing.T) { 108 if err == nil || ctx.Err() != context.DeadlineExceeded { 109 t.Errorf("err %v; want %v", err, context.DeadlineExceeded) 110 } 111 }