Skip to content

Commit

Permalink
Merge pull request #319 from asamuzak09/feature/add_validate_rich_men…
Browse files Browse the repository at this point in the history
…u_object

add ValidateRichMenuObjectMethod
  • Loading branch information
kkdai committed Jul 21, 2022
2 parents fbd11c1 + 36e4f33 commit 22a7e32
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions linebot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const (
APIEndpointUploadRichMenuImage = "/v2/bot/richmenu/%s/content" // Download: GET / Upload: POST
APIEndpointBulkLinkRichMenu = "/v2/bot/richmenu/bulk/link"
APIEndpointBulkUnlinkRichMenu = "/v2/bot/richmenu/bulk/unlink"
APIEndpointValidateRichMenuObject = "/v2/bot/richmenu/validate"

APIEndpointCreateRichMenuAlias = "/v2/bot/richmenu/alias"
APIEndpointGetRichMenuAlias = "/v2/bot/richmenu/alias/%s"
Expand Down
53 changes: 53 additions & 0 deletions linebot/richmenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,3 +825,56 @@ func (call *GetRichMenuAliasListCall) Do() ([]*RichMenuAliasResponse, error) {
defer closeResponse(res)
return decodeToRichMenuAliasListResponse(res)
}

// ValidateRichMenuObject method
func (client *Client) ValidateRichMenuObject(richMenu RichMenu) *ValidateRichMenuObjectCall {
return &ValidateRichMenuObjectCall{
c: client,
richMenu: richMenu,
}
}

// ValidateRichMenuObjectCall type
type ValidateRichMenuObjectCall struct {
c *Client
ctx context.Context

richMenu RichMenu
}

// WithContext method
func (call *ValidateRichMenuObjectCall) WithContext(ctx context.Context) *ValidateRichMenuObjectCall {
call.ctx = ctx
return call
}

func (call *ValidateRichMenuObjectCall) encodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(&struct {
Size RichMenuSize `json:"size"`
Selected bool `json:"selected"`
Name string `json:"name"`
ChatBarText string `json:"chatBarText"`
Areas []AreaDetail `json:"areas"`
}{
Size: call.richMenu.Size,
Selected: call.richMenu.Selected,
Name: call.richMenu.Name,
ChatBarText: call.richMenu.ChatBarText,
Areas: call.richMenu.Areas,
})
}

// Do method
func (call *ValidateRichMenuObjectCall) Do() (*BasicResponse, error) {
var buf bytes.Buffer
if err := call.encodeJSON(&buf); err != nil {
return nil, err
}
res, err := call.c.post(call.ctx, APIEndpointValidateRichMenuObject, &buf)
if err != nil {
return nil, err
}
defer closeResponse(res)
return decodeToBasicResponse(res)
}
90 changes: 90 additions & 0 deletions linebot/richmenu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,3 +1101,93 @@ func TestListRichMenuAlias(t *testing.T) {
})
}
}

func TestValidateRichMenuObject(t *testing.T) {
type want struct {
RequestBody []byte
Response *BasicResponse
Error error
}
testCases := []struct {
Request RichMenu
Response []byte
ResponseCode int
Want want
}{
{
Request: RichMenu{
Size: RichMenuSize{Width: 2500, Height: 1686},
Selected: false,
Name: "Menu1",
ChatBarText: "ChatText",
Areas: []AreaDetail{
{
Bounds: RichMenuBounds{X: 0, Y: 0, Width: 2500, Height: 1686},
Action: RichMenuAction{Type: RichMenuActionTypePostback, Data: "action=buy&itemid=123", DisplayText: "Buy", Label: "Buy"},
},
},
},
ResponseCode: 200,
Response: []byte(`{}`),
Want: want{
RequestBody: []byte(`{"size":{"width":2500,"height":1686},"selected":false,"name":"Menu1","chatBarText":"ChatText","areas":[{"bounds":{"x":0,"y":0,"width":2500,"height":1686},"action":{"type":"postback","displayText":"Buy","label":"Buy","data":"action=buy\u0026itemid=123"}}]}` + "\n"),
Response: &BasicResponse{},
},
},
}

var currentTestIdx int
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
tc := testCases[currentTestIdx]
if r.Method != http.MethodPost {
t.Errorf("Method %s; want %s", r.Method, http.MethodPost)
}
if r.URL.Path != APIEndpointValidateRichMenuObject {
t.Errorf("URLPath %s; want %s", r.URL.Path, APIEndpointValidateRichMenuObject)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(body, tc.Want.RequestBody) {
t.Errorf("RequestBody\n %s; want\n %s", body, tc.Want.RequestBody)
}
w.WriteHeader(tc.ResponseCode)
w.Write(tc.Response)
}))
defer server.Close()

dataServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
t.Error("Unexpected data API call")
w.WriteHeader(404)
w.Write([]byte(`{"message":"Not found"}`))
}))
defer dataServer.Close()

client, err := mockClient(server, dataServer)
if err != nil {
t.Fatal(err)
}
for i, tc := range testCases {
currentTestIdx = i
t.Run(strconv.Itoa(i), func(t *testing.T) {
res, err := client.ValidateRichMenuObject(tc.Request).Do()
if tc.Want.Error != nil {
if !reflect.DeepEqual(err, tc.Want.Error) {
t.Errorf("Error %v; want %v", err, tc.Want.Error)
}
} else {
if err != nil {
t.Error(err)
}
}
if tc.Want.Response != nil {
if !reflect.DeepEqual(res, tc.Want.Response) {
t.Errorf("Response %v; want %v", res, tc.Want.Response)
}
}
})
}
}

0 comments on commit 22a7e32

Please sign in to comment.