Skip to content

Commit fbf8494

Browse files
committed
tests added for Top cmd
* doc added for Top * readme updated * example Top function added in example/main.go
1 parent 046711e commit fbf8494

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ development. [RFC 1939](https://www.ietf.org/rfc/rfc1939.txt) document has been
1616
* NOOP
1717
* RSET
1818
* QUIT
19+
* TOP
1920

2021
### Installation
2122

@@ -79,6 +80,10 @@ func main() {
7980
// LIST <mail-num> command
8081
ll, _ := pop.List(1)
8182
fmt.Println(ll[0])
83+
84+
// TOP msgNum n
85+
top, _ := pop.Top(1, 10)
86+
fmt.Println(top)
8287

8388
// DELE command
8489
dele, err := pop.Dele("1")

example/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func main() {
4444
list, _ = pop.List(1) // LIST <arg> command
4545
fmt.Println(list) // 1st message size
4646

47+
top, _ := pop.Top(1, 10) // TOP msgNum n
48+
fmt.Println(top) // Headers, blank line, and top 10 line of the message body in an array
49+
4750
n, _ := pop.Noop() // NOOP command
4851
fmt.Println(n) // +OK
4952

pop3/transaction.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,31 @@ func (c *Client) pass(password string) (string, error) {
397397
return passResp, nil
398398
}
399399

400+
// Top is a command which fetches message (msgNum) with n lines. To get messages
401+
// from mail server, you need to authenticate. The response starts with "+OK"
402+
// status indicator, and it follows the multiline mail. The server sends header
403+
// of the message, the blank line separating the headers from the body, and then
404+
// the number of lines of the message's body. If you request the message line
405+
// number greater than the number of lines in the body, the mail server sends
406+
// the entire message.
407+
// Possible responses:
408+
// +OK message follows
409+
// -ERR no such message
410+
// Example:
411+
// C: TOP 1 10
412+
// S: +OK message follows
413+
// S: <headers of the message, blank line, and the first 10 lines of the body>
414+
// S: .
415+
// ...
416+
// C: TOP 100 10
417+
// S: -ERR no such message
418+
//
419+
// msgNum indicates message id starts from 1 and n is line of the message's body.
400420
func (c *Client) Top(msgNum, n int) ([]string, error) {
401421
return c.top(msgNum, n)
402422
}
403423

424+
// top is the implementation function of the Top function.
404425
func (c *Client) top(msgNum, n int) ([]string, error) {
405426
if msgNum < 1 {
406427
return nil, fmt.Errorf("%s message number should be greater than 0", e)

pop3/transaction_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,63 @@ func TestTopNegativeN(t *testing.T) {
549549
}
550550
log.Println("Connection closed")
551551
}
552+
553+
func TestTopNotLoggedIn(t *testing.T) {
554+
pop, err := Connect(gmailTLSAddr, nil, true)
555+
if err != nil {
556+
t.Errorf(err.Error())
557+
}
558+
log.Println("Connection established")
559+
560+
top, err := pop.Top(1, 20)
561+
if err != nil {
562+
t.Errorf("err need to be <nil>")
563+
}
564+
if !strings.HasPrefix(top[0], e) {
565+
t.Errorf(top[0])
566+
}
567+
log.Println(top[0])
568+
log.Println(err)
569+
}
570+
571+
func TestTopPass(t *testing.T) {
572+
pop, err := Connect(gmailTLSAddr, nil, true)
573+
if err != nil {
574+
t.Errorf(err.Error())
575+
}
576+
log.Println("Connection established")
577+
578+
username := os.Getenv(userKey)
579+
u, err := pop.User(username)
580+
if err != nil {
581+
t.Errorf(err.Error())
582+
}
583+
if !strings.HasPrefix(u, ok) {
584+
t.Errorf("expected: %s, got: %s", ok, u)
585+
}
586+
587+
password := os.Getenv(passwordKey)
588+
p, err := pop.Pass(password)
589+
if err != nil {
590+
t.Errorf(err.Error())
591+
}
592+
if !strings.HasPrefix(p, ok) {
593+
t.Errorf("expected: %s, got: %s", ok, p)
594+
}
595+
596+
top, err := pop.Top(1, 20)
597+
if err != nil {
598+
t.Errorf(err.Error())
599+
}
600+
if !strings.HasPrefix(top[0], ok) {
601+
t.Errorf(top[0])
602+
}
603+
log.Println(top[0])
604+
605+
quit, err := pop.Quit()
606+
if err != nil {
607+
t.Errorf(err.Error())
608+
}
609+
log.Println(quit)
610+
log.Println("Connection closed")
611+
}

0 commit comments

Comments
 (0)