github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/get_bot_info_test.go (about) 1 // Copyright 2020 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 "io" 20 "net/http" 21 "net/http/httptest" 22 "reflect" 23 "strconv" 24 "testing" 25 "time" 26 ) 27 28 func TestGetBotInfo(t *testing.T) { 29 type want struct { 30 URLPath string 31 RequestBody []byte 32 Response *BotInfoResponse 33 Error error 34 } 35 testCases := []struct { 36 Label string 37 ResponseCode int 38 Response []byte 39 Want want 40 }{ 41 { 42 Label: "Success", 43 ResponseCode: 200, 44 Response: []byte(`{"userId":"u206d25c2ea6bd87c17655609a1c37cb8","basicId":"@012abcde","premiumId":"PremiumId","displayName":"BotTest","pictureUrl":"https://example.com/abcdefghijklmn","chatMode":"chat","markAsReadMode":"manual"}`), 45 Want: want{ 46 URLPath: APIEndpointGetBotInfo, 47 RequestBody: []byte(""), 48 Response: &BotInfoResponse{ 49 UserID: "u206d25c2ea6bd87c17655609a1c37cb8", 50 BasicID: "@012abcde", 51 PremiumID: "PremiumId", 52 DisplayName: "BotTest", 53 PictureURL: "https://example.com/abcdefghijklmn", 54 ChatMode: ChatModeChat, 55 MarkAsReadMode: MarkAsReadModeManual, 56 }, 57 }, 58 }, 59 { 60 Label: "No premiumID Success", 61 ResponseCode: 200, 62 Response: []byte(`{"userId":"u206d25c2ea6bd87c17655609a1c37cb8","basicId":"@012abcde","displayName":"BotTest","pictureUrl":"https://example.com/abcdefghijklmn","chatMode":"bot","markAsReadMode":"auto"}`), 63 Want: want{ 64 URLPath: APIEndpointGetBotInfo, 65 RequestBody: []byte(""), 66 Response: &BotInfoResponse{ 67 UserID: "u206d25c2ea6bd87c17655609a1c37cb8", 68 BasicID: "@012abcde", 69 PremiumID: "", 70 DisplayName: "BotTest", 71 PictureURL: "https://example.com/abcdefghijklmn", 72 ChatMode: ChatModeBot, 73 MarkAsReadMode: MarkAsReadModeAuto, 74 }, 75 }, 76 }, 77 { 78 Label: "Internal server error", 79 ResponseCode: 500, 80 Response: []byte("500 Internal server error"), 81 Want: want{ 82 URLPath: APIEndpointGetBotInfo, 83 RequestBody: []byte(""), 84 Error: &APIError{ 85 Code: 500, 86 }, 87 }, 88 }, 89 { 90 Label: "Invalid channelAccessToken error", 91 ResponseCode: 401, 92 Response: []byte(`{"message":"Authentication failed due to the following reason: invalid token. Confirm that the access token in the authorization header is valid."}`), 93 Want: want{ 94 URLPath: APIEndpointGetBotInfo, 95 RequestBody: []byte(""), 96 Error: &APIError{ 97 Code: 401, 98 Response: &ErrorResponse{ 99 Message: "Authentication failed due to the following reason: invalid token. Confirm that the access token in the authorization header is valid.", 100 }, 101 }, 102 }, 103 }, 104 } 105 106 var currentTestIdx int 107 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 108 defer r.Body.Close() 109 tc := testCases[currentTestIdx] 110 if r.Method != http.MethodGet { 111 t.Errorf("Method %s; want %s", r.Method, http.MethodGet) 112 } 113 if r.URL.Path != tc.Want.URLPath { 114 t.Errorf("URLPath %s; want %s", r.URL.Path, tc.Want.URLPath) 115 } 116 body, err := io.ReadAll(r.Body) 117 if err != nil { 118 t.Fatal(err) 119 } 120 if !reflect.DeepEqual(body, tc.Want.RequestBody) { 121 t.Errorf("RequestBody %s; want %s", body, tc.Want.RequestBody) 122 } 123 w.WriteHeader(tc.ResponseCode) 124 w.Write(tc.Response) 125 })) 126 defer server.Close() 127 128 dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 129 defer r.Body.Close() 130 t.Error("Unexpected Data API call") 131 w.WriteHeader(404) 132 w.Write([]byte(`{"message":"Not found"}`)) 133 })) 134 defer dataServer.Close() 135 136 client, err := mockClient(server, dataServer) 137 if err != nil { 138 t.Fatal(err) 139 } 140 for i, tc := range testCases { 141 currentTestIdx = i 142 t.Run(strconv.Itoa(i)+"/"+tc.Label, func(t *testing.T) { 143 res, err := client.GetBotInfo().Do() 144 if tc.Want.Error != nil { 145 if !reflect.DeepEqual(err, tc.Want.Error) { 146 t.Errorf("Error %v; want %v", err, tc.Want.Error) 147 } 148 } else { 149 if err != nil { 150 t.Error(err) 151 } 152 } 153 if !reflect.DeepEqual(res, tc.Want.Response) { 154 t.Errorf("Response %v; want %v", res, tc.Want.Response) 155 } 156 }) 157 } 158 } 159 160 func TestGetBotInfoWithContext(t *testing.T) { 161 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 162 defer r.Body.Close() 163 time.Sleep(10 * time.Millisecond) 164 w.Write([]byte("{}")) 165 })) 166 defer server.Close() 167 168 dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 169 defer r.Body.Close() 170 t.Error("Unexpected Data API call") 171 w.WriteHeader(404) 172 w.Write([]byte(`{"message":"Not found"}`)) 173 })) 174 defer dataServer.Close() 175 176 client, err := mockClient(server, dataServer) 177 if err != nil { 178 t.Fatal(err) 179 } 180 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond) 181 defer cancel() 182 _, err = client.GetBotInfo().WithContext(ctx).Do() 183 expectCtxDeadlineExceed(ctx, err, t) 184 } 185 186 func BenchmarkGetBotInfo(b *testing.B) { 187 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 188 defer r.Body.Close() 189 w.Write([]byte(`{"userId":"U","basicId":"@B","premiumId":"P","displayName":"BotTest","pictureUrl":"https://","chatMode":"chat","markAsReadMode":"manual"}`)) 190 })) 191 defer server.Close() 192 193 dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 194 defer r.Body.Close() 195 b.Error("Unexpected Data API call") 196 w.WriteHeader(404) 197 w.Write([]byte(`{"message":"Not found"}`)) 198 })) 199 defer dataServer.Close() 200 201 client, err := mockClient(server, dataServer) 202 if err != nil { 203 b.Fatal(err) 204 } 205 b.ResetTimer() 206 for i := 0; i < b.N; i++ { 207 client.GetBotInfo().Do() 208 } 209 }