github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/delivery_test.go (about) 1 // Copyright 2019 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 "testing" 25 ) 26 27 // TestGetNumberMessages tests GetNumberReplyMessages, GetNumberPushMessages, GetNumberMulticastMessages 28 // and GetNumberBroadcastMessages func 29 func TestGetNumberMessages(t *testing.T) { 30 type want struct { 31 URLPath string 32 RequestBody []byte 33 Response *MessagesNumberResponse 34 Error error 35 } 36 testCases := []struct { 37 TestType DeliveryType 38 Date string 39 ResponseCode int 40 Response []byte 41 Want want 42 }{ 43 { 44 Date: "20190403", 45 TestType: DeliveryTypeReply, 46 ResponseCode: 200, 47 Response: []byte(`{"status":"ready","success":123}`), 48 Want: want{ 49 URLPath: fmt.Sprintf(APIEndpointGetMessageDelivery, DeliveryTypeReply), 50 Response: &MessagesNumberResponse{ 51 Status: "ready", 52 Success: 123, 53 }, 54 }, 55 }, 56 { 57 Date: "20180330", 58 TestType: DeliveryTypeReply, 59 ResponseCode: 200, 60 Response: []byte(`{"status":"out_of_service"}`), 61 Want: want{ 62 URLPath: fmt.Sprintf(APIEndpointGetMessageDelivery, DeliveryTypeReply), 63 Response: &MessagesNumberResponse{ 64 Status: "out_of_service", 65 }, 66 }, 67 }, 68 { 69 Date: "20290403", 70 TestType: DeliveryTypePush, 71 ResponseCode: 200, 72 Response: []byte(`{"status":"unready"}`), 73 Want: want{ 74 URLPath: fmt.Sprintf(APIEndpointGetMessageDelivery, DeliveryTypePush), 75 Response: &MessagesNumberResponse{ 76 Status: "unready", 77 }, 78 }, 79 }, 80 { 81 Date: "20190401", 82 TestType: DeliveryTypeMulticast, 83 ResponseCode: 200, 84 Response: []byte(`{"status":"ready","success":456}`), 85 Want: want{ 86 URLPath: fmt.Sprintf(APIEndpointGetMessageDelivery, DeliveryTypeMulticast), 87 Response: &MessagesNumberResponse{ 88 Status: "ready", 89 Success: 456, 90 }, 91 }, 92 }, 93 } 94 95 var currentTestIdx int 96 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 97 defer r.Body.Close() 98 tc := testCases[currentTestIdx] 99 if r.Method != http.MethodGet { 100 t.Errorf("Method %s; want %s", r.Method, http.MethodGet) 101 } 102 if r.URL.Path != tc.Want.URLPath { 103 t.Errorf("URLPath %s; want %s", r.URL.Path, tc.Want.URLPath) 104 } 105 _, err := io.ReadAll(r.Body) 106 if err != nil { 107 t.Fatal(err) 108 } 109 w.WriteHeader(tc.ResponseCode) 110 w.Write(tc.Response) 111 })) 112 defer server.Close() 113 114 dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 115 defer r.Body.Close() 116 t.Error("Unexpected data API call") 117 w.WriteHeader(404) 118 w.Write([]byte(`{"message":"Not found"}`)) 119 })) 120 defer dataServer.Close() 121 122 client, err := mockClient(server, dataServer) 123 if err != nil { 124 t.Fatal(err) 125 } 126 var res interface{} 127 for i, tc := range testCases { 128 currentTestIdx = i 129 t.Run(strconv.Itoa(i)+"/"+string(tc.TestType)+"."+tc.Date, func(t *testing.T) { 130 switch tc.TestType { 131 case DeliveryTypeMulticast: 132 res, err = client.GetNumberMulticastMessages(tc.Date).Do() 133 case DeliveryTypePush: 134 res, err = client.GetNumberPushMessages(tc.Date).Do() 135 case DeliveryTypeReply: 136 res, err = client.GetNumberReplyMessages(tc.Date).Do() 137 case DeliveryTypeBroadcast: 138 res, err = client.GetNumberBroadcastMessages(tc.Date).Do() 139 } 140 if tc.Want.Error != nil { 141 if !reflect.DeepEqual(err, tc.Want.Error) { 142 t.Errorf("Error %v; want %v", err, tc.Want.Error) 143 } 144 } else { 145 if err != nil { 146 t.Error(err) 147 } 148 } 149 if tc.Want.Response != nil { 150 if !reflect.DeepEqual(res, tc.Want.Response) { 151 t.Errorf("Response %v; want %v", res, tc.Want.Response) 152 } 153 } 154 }) 155 } 156 }