github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/get_summary_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  func TestGetGroupSummary(t *testing.T) {
    30  	type want struct {
    31  		URLPath     string
    32  		RequestBody []byte
    33  		Response    *GroupSummaryResponse
    34  		Error       error
    35  	}
    36  	testCases := []struct {
    37  		Label        string
    38  		GroupID      string
    39  		ResponseCode int
    40  		Response     []byte
    41  		Want         want
    42  	}{
    43  		{
    44  			Label:        "Success",
    45  			GroupID:      "Ca56f94637c",
    46  			ResponseCode: 200,
    47  			Response:     []byte(`{"groupId":"Ca56f94637c","groupName":"Group name","pictureUrl":"https://example.com/abcdefghijklmn"}`),
    48  			Want: want{
    49  				URLPath:     fmt.Sprintf(APIEndpointGetGroupSummary, "Ca56f94637c"),
    50  				RequestBody: []byte(""),
    51  				Response: &GroupSummaryResponse{
    52  					GroupID:    "Ca56f94637c",
    53  					GroupName:  "Group name",
    54  					PictureURL: "https://example.com/abcdefghijklmn",
    55  				},
    56  			},
    57  		},
    58  		{
    59  			Label:        "Internal server error",
    60  			GroupID:      "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    61  			ResponseCode: 500,
    62  			Response:     []byte("500 Internal server error"),
    63  			Want: want{
    64  				URLPath:     fmt.Sprintf(APIEndpointGetGroupSummary, "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
    65  				RequestBody: []byte(""),
    66  				Error: &APIError{
    67  					Code: 500,
    68  				},
    69  			},
    70  		},
    71  	}
    72  
    73  	var currentTestIdx int
    74  	server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    75  		defer r.Body.Close()
    76  		tc := testCases[currentTestIdx]
    77  		if r.Method != http.MethodGet {
    78  			t.Errorf("Method %s; want %s", r.Method, http.MethodGet)
    79  		}
    80  		if r.URL.Path != tc.Want.URLPath {
    81  			t.Errorf("URLPath %s; want %s", r.URL.Path, tc.Want.URLPath)
    82  		}
    83  		body, err := io.ReadAll(r.Body)
    84  		if err != nil {
    85  			t.Fatal(err)
    86  		}
    87  		if !reflect.DeepEqual(body, tc.Want.RequestBody) {
    88  			t.Errorf("RequestBody %s; want %s", body, tc.Want.RequestBody)
    89  		}
    90  		w.WriteHeader(tc.ResponseCode)
    91  		w.Write(tc.Response)
    92  	}))
    93  	defer server.Close()
    94  
    95  	dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    96  		defer r.Body.Close()
    97  		t.Error("Unexpected Data API call")
    98  		w.WriteHeader(404)
    99  		w.Write([]byte(`{"message":"Not found"}`))
   100  	}))
   101  	defer dataServer.Close()
   102  
   103  	client, err := mockClient(server, dataServer)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	for i, tc := range testCases {
   108  		currentTestIdx = i
   109  		t.Run(strconv.Itoa(i)+"/"+tc.Label, func(t *testing.T) {
   110  			res, err := client.GetGroupSummary(tc.GroupID).Do()
   111  			if tc.Want.Error != nil {
   112  				if !reflect.DeepEqual(err, tc.Want.Error) {
   113  					t.Errorf("Error %v; want %v", err, tc.Want.Error)
   114  				}
   115  			} else {
   116  				if err != nil {
   117  					t.Error(err)
   118  				}
   119  			}
   120  			if !reflect.DeepEqual(res, tc.Want.Response) {
   121  				t.Errorf("Response %v; want %v", res, tc.Want.Response)
   122  			}
   123  		})
   124  	}
   125  }
   126  
   127  func TestGetGroupSummaryWithContext(t *testing.T) {
   128  	server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   129  		defer r.Body.Close()
   130  		time.Sleep(10 * time.Millisecond)
   131  		w.Write([]byte("{}"))
   132  	}))
   133  	defer server.Close()
   134  
   135  	dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   136  		defer r.Body.Close()
   137  		t.Error("Unexpected Data API call")
   138  		w.WriteHeader(404)
   139  		w.Write([]byte(`{"message":"Not found"}`))
   140  	}))
   141  	defer dataServer.Close()
   142  
   143  	client, err := mockClient(server, dataServer)
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
   148  	defer cancel()
   149  	_, err = client.GetGroupSummary("cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").WithContext(ctx).Do()
   150  	expectCtxDeadlineExceed(ctx, err, t)
   151  }
   152  
   153  func BenchmarkGetGroupSummary(b *testing.B) {
   154  	server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   155  		defer r.Body.Close()
   156  		w.Write([]byte(`{"groupId":"G","groupName":"A","pictureUrl":"https://"}`))
   157  	}))
   158  	defer server.Close()
   159  
   160  	dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   161  		defer r.Body.Close()
   162  		b.Error("Unexpected Data API call")
   163  		w.WriteHeader(404)
   164  		w.Write([]byte(`{"message":"Not found"}`))
   165  	}))
   166  	defer dataServer.Close()
   167  
   168  	client, err := mockClient(server, dataServer)
   169  	if err != nil {
   170  		b.Fatal(err)
   171  	}
   172  	b.ResetTimer()
   173  	for i := 0; i < b.N; i++ {
   174  		client.GetGroupSummary("cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").Do()
   175  	}
   176  }