Skip to content

Commit

Permalink
Top command add
Browse files Browse the repository at this point in the history
* invalid parameter tests added
  • Loading branch information
gozeloglu committed Sep 28, 2021
1 parent 8a6a1da commit 046711e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pop3/transaction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pop3

import (
"fmt"
"strconv"
"strings"
)
Expand Down Expand Up @@ -395,3 +396,24 @@ func (c *Client) pass(password string) (string, error) {

return passResp, nil
}

func (c *Client) Top(msgNum, n int) ([]string, error) {
return c.top(msgNum, n)
}

func (c *Client) top(msgNum, n int) ([]string, error) {
if msgNum < 1 {
return nil, fmt.Errorf("%s message number should be greater than 0", e)
}
if n < 0 {
return nil, fmt.Errorf("%s line count cannot be negative", e)
}
cmd := "TOP"
arg := fmt.Sprintf("%d %d", msgNum, n)
err := c.sendCmdWithArg(cmd, arg)
if err != nil {
return nil, err
}

return c.readRespMultiLines()
}
57 changes: 57 additions & 0 deletions pop3/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pop3

import (
"log"
"math"
"os"
"strconv"
Expand Down Expand Up @@ -492,3 +493,59 @@ func TestRset(t *testing.T) {
t.Errorf("expected prefix: %s, got: %s", ok, r)
}
}

func TestTopNegativeMsgNum(t *testing.T) {
exp := "-ERR message number should be greater than 0"
pop, err := Connect(gmailTLSAddr, nil, true)
if err != nil {
t.Errorf(err.Error())
}
log.Println("Connection established")

msgNum := -1
top, err := pop.Top(msgNum, 10)
if top != nil {
t.Errorf("need to be nil")
}
if !strings.HasPrefix(err.Error(), exp) {
t.Errorf(err.Error())
}
log.Println(err.Error())

quit, err := pop.Quit()
if err != nil {
t.Errorf(err.Error())
}
if !strings.HasPrefix(quit, ok) {
t.Errorf(quit)
}
log.Println("Connection closed")
}

func TestTopNegativeN(t *testing.T) {
exp := "-ERR line count cannot be negative"
pop, err := Connect(gmailTLSAddr, nil, true)
if err != nil {
t.Errorf(err.Error())
}
log.Println("Connection established")

n := -1
top, err := pop.Top(1, n)
if top != nil {
t.Errorf("need to be nil")
}
if !strings.HasPrefix(err.Error(), exp) {
t.Errorf(err.Error())
}
log.Println(err.Error())

quit, err := pop.Quit()
if err != nil {
t.Errorf(err.Error())
}
if !strings.HasPrefix(quit, ok) {
t.Errorf(quit)
}
log.Println("Connection closed")
}

0 comments on commit 046711e

Please sign in to comment.