Skip to content

Commit 046711e

Browse files
committed
Top command add
* invalid parameter tests added
1 parent 8a6a1da commit 046711e

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

pop3/transaction.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pop3
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67
)
@@ -395,3 +396,24 @@ func (c *Client) pass(password string) (string, error) {
395396

396397
return passResp, nil
397398
}
399+
400+
func (c *Client) Top(msgNum, n int) ([]string, error) {
401+
return c.top(msgNum, n)
402+
}
403+
404+
func (c *Client) top(msgNum, n int) ([]string, error) {
405+
if msgNum < 1 {
406+
return nil, fmt.Errorf("%s message number should be greater than 0", e)
407+
}
408+
if n < 0 {
409+
return nil, fmt.Errorf("%s line count cannot be negative", e)
410+
}
411+
cmd := "TOP"
412+
arg := fmt.Sprintf("%d %d", msgNum, n)
413+
err := c.sendCmdWithArg(cmd, arg)
414+
if err != nil {
415+
return nil, err
416+
}
417+
418+
return c.readRespMultiLines()
419+
}

pop3/transaction_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pop3
22

33
import (
4+
"log"
45
"math"
56
"os"
67
"strconv"
@@ -492,3 +493,59 @@ func TestRset(t *testing.T) {
492493
t.Errorf("expected prefix: %s, got: %s", ok, r)
493494
}
494495
}
496+
497+
func TestTopNegativeMsgNum(t *testing.T) {
498+
exp := "-ERR message number should be greater than 0"
499+
pop, err := Connect(gmailTLSAddr, nil, true)
500+
if err != nil {
501+
t.Errorf(err.Error())
502+
}
503+
log.Println("Connection established")
504+
505+
msgNum := -1
506+
top, err := pop.Top(msgNum, 10)
507+
if top != nil {
508+
t.Errorf("need to be nil")
509+
}
510+
if !strings.HasPrefix(err.Error(), exp) {
511+
t.Errorf(err.Error())
512+
}
513+
log.Println(err.Error())
514+
515+
quit, err := pop.Quit()
516+
if err != nil {
517+
t.Errorf(err.Error())
518+
}
519+
if !strings.HasPrefix(quit, ok) {
520+
t.Errorf(quit)
521+
}
522+
log.Println("Connection closed")
523+
}
524+
525+
func TestTopNegativeN(t *testing.T) {
526+
exp := "-ERR line count cannot be negative"
527+
pop, err := Connect(gmailTLSAddr, nil, true)
528+
if err != nil {
529+
t.Errorf(err.Error())
530+
}
531+
log.Println("Connection established")
532+
533+
n := -1
534+
top, err := pop.Top(1, n)
535+
if top != nil {
536+
t.Errorf("need to be nil")
537+
}
538+
if !strings.HasPrefix(err.Error(), exp) {
539+
t.Errorf(err.Error())
540+
}
541+
log.Println(err.Error())
542+
543+
quit, err := pop.Quit()
544+
if err != nil {
545+
t.Errorf(err.Error())
546+
}
547+
if !strings.HasPrefix(quit, ok) {
548+
t.Errorf(quit)
549+
}
550+
log.Println("Connection closed")
551+
}

0 commit comments

Comments
 (0)