github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/progress_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 "fmt" 20 "io" 21 "net/http" 22 "net/http/httptest" 23 "reflect" 24 "strconv" 25 "testing" 26 "time" 27 ) 28 29 // TestGetProgressMessages tests GetProgressNarrowcastMessages func 30 func TestGetProgressMessages(t *testing.T) { 31 type want struct { 32 URLPath string 33 RequestBody []byte 34 Response *MessagesProgressResponse 35 Error error 36 } 37 testCases := []struct { 38 TestType ProgressType 39 RequestID string 40 ResponseCode int 41 Response []byte 42 Want want 43 }{ 44 { 45 RequestID: "f70dd685-499a-1", 46 TestType: ProgressTypeNarrowcast, 47 ResponseCode: 200, 48 Response: []byte(`{"phase":"waiting","acceptedTime":"2020-12-03T10:15:30.121Z"}`), 49 Want: want{ 50 URLPath: fmt.Sprintf(APIEndpointGetMessageProgress, ProgressTypeNarrowcast), 51 Response: &MessagesProgressResponse{ 52 Phase: "waiting", 53 SuccessCount: 0, 54 FailureCount: 0, 55 TargetCount: 0, 56 FailedDescription: "", 57 ErrorCode: 0, 58 AcceptedTime: "2020-12-03T10:15:30.121Z", 59 CompletedTime: "", 60 }, 61 }, 62 }, 63 { 64 RequestID: "f70dd685-499a-2", 65 TestType: ProgressTypeNarrowcast, 66 ResponseCode: 200, 67 Response: []byte(`{"phase":"succeeded","successCount":10,"failureCount":0,"targetCount":10,"acceptedTime":"2020-12-03T10:15:30.121Z","completedTime":"2020-12-03T10:15:30.121Z"}`), 68 Want: want{ 69 URLPath: fmt.Sprintf(APIEndpointGetMessageProgress, ProgressTypeNarrowcast), 70 Response: &MessagesProgressResponse{ 71 Phase: "succeeded", 72 SuccessCount: 10, 73 FailureCount: 0, 74 TargetCount: 10, 75 FailedDescription: "", 76 ErrorCode: 0, 77 AcceptedTime: "2020-12-03T10:15:30.121Z", 78 CompletedTime: "2020-12-03T10:15:30.121Z", 79 }, 80 }, 81 }, 82 { 83 RequestID: "f70dd685-499a-3", 84 TestType: ProgressTypeNarrowcast, 85 ResponseCode: 200, 86 Response: []byte(`{"phase":"failed","failedDescription":"internal error","errorCode":1,"acceptedTime":"2020-12-03T10:15:30.121Z","completedTime":"2020-12-03T10:15:30.121Z"}`), 87 Want: want{ 88 URLPath: fmt.Sprintf(APIEndpointGetMessageProgress, ProgressTypeNarrowcast), 89 Response: &MessagesProgressResponse{ 90 Phase: "failed", 91 SuccessCount: 0, 92 FailureCount: 0, 93 TargetCount: 0, 94 FailedDescription: "internal error", 95 ErrorCode: 1, 96 AcceptedTime: "2020-12-03T10:15:30.121Z", 97 CompletedTime: "2020-12-03T10:15:30.121Z", 98 }, 99 }, 100 }, 101 { 102 RequestID: "f70dd685-499a-4", 103 TestType: ProgressTypeNarrowcast, 104 ResponseCode: 404, 105 Response: []byte(`invalid request ID`), 106 Want: want{ 107 URLPath: fmt.Sprintf(APIEndpointGetMessageProgress, ProgressTypeNarrowcast), 108 Error: &APIError{ 109 Code: 404, 110 }, 111 }, 112 }, 113 } 114 115 var currentTestIdx int 116 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 117 defer r.Body.Close() 118 tc := testCases[currentTestIdx] 119 if r.Method != http.MethodGet { 120 t.Errorf("Method %s; want %s", r.Method, http.MethodGet) 121 } 122 if r.URL.Path != tc.Want.URLPath { 123 t.Errorf("URLPath %s; want %s", r.URL.Path, tc.Want.URLPath) 124 } 125 _, err := io.ReadAll(r.Body) 126 if err != nil { 127 t.Fatal(err) 128 } 129 w.WriteHeader(tc.ResponseCode) 130 w.Write(tc.Response) 131 })) 132 defer server.Close() 133 134 dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 135 defer r.Body.Close() 136 t.Error("Unexpected data API call") 137 w.WriteHeader(404) 138 w.Write([]byte(`{"message":"Not found"}`)) 139 })) 140 defer dataServer.Close() 141 142 client, err := mockClient(server, dataServer) 143 if err != nil { 144 t.Fatal(err) 145 } 146 var res interface{} 147 for i, tc := range testCases { 148 currentTestIdx = i 149 t.Run(strconv.Itoa(i)+"/"+string(tc.TestType)+"."+tc.RequestID, func(t *testing.T) { 150 switch tc.TestType { 151 case ProgressTypeNarrowcast: 152 res, err = client.GetProgressNarrowcastMessages(tc.RequestID).Do() 153 } 154 if tc.Want.Error != nil { 155 if !reflect.DeepEqual(err, tc.Want.Error) { 156 t.Errorf("Error %v; want %v", err, tc.Want.Error) 157 } 158 } else { 159 if err != nil { 160 t.Error(err) 161 } 162 } 163 if tc.Want.Response != nil { 164 if !reflect.DeepEqual(res, tc.Want.Response) { 165 t.Errorf("Response %v; want %v", res, tc.Want.Response) 166 } 167 } 168 }) 169 } 170 } 171 172 func TestGetProgressMessagesWithContext(t *testing.T) { 173 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 174 defer r.Body.Close() 175 time.Sleep(10 * time.Millisecond) 176 w.Write([]byte("{}")) 177 })) 178 defer server.Close() 179 180 dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 181 defer r.Body.Close() 182 t.Error("Unexpected data API call") 183 w.WriteHeader(404) 184 w.Write([]byte(`{"message":"Not found"}`)) 185 })) 186 defer dataServer.Close() 187 188 client, err := mockClient(server, dataServer) 189 if err != nil { 190 t.Fatal(err) 191 } 192 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond) 193 defer cancel() 194 _, err = client.GetProgressNarrowcastMessages("f70dd685-499a-0").WithContext(ctx).Do() 195 expectCtxDeadlineExceed(ctx, err, t) 196 }