-
Notifications
You must be signed in to change notification settings - Fork 0
/
pastebin_test.go
77 lines (55 loc) · 1.93 KB
/
pastebin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package pastebin
import (
"log"
"os"
"regexp"
"testing"
"github.com/mfvitale/pastebin-go/model"
)
var client *Client
var err error
func init() {
/* load test data */
client, err = NewClient(os.Getenv("DEV_KEY"), os.Getenv("USERNAME"), os.Getenv("PASSWORD"))
log.Println("Creating client")
if err != nil {
log.Fatalln("Error creating client ", err)
}
}
func TestAnonymPasteCreation(t *testing.T) {
anonClient := NewAnonymousClient(os.Getenv("DEV_KEY"))
paste := model.FullPaste("Test paste bin", model.Public, "npm run", model.TEN_MINUTES, "bash")
res, err := anonClient.CreatePaste(paste)
want := regexp.MustCompile(`https://pastebin.com/([a-zA-Z0-9_]*)`)
if !want.MatchString(res) || err != nil {
t.Fatalf(`anonClient.CreatePaste(paste) = %q, %v, want match for %#q, nil`, res, err, want)
}
}
func TestLoggedPasteCreation(t *testing.T) {
paste := model.FullPaste("Test paste bin", model.Public, "npm run", model.TEN_MINUTES, "bash")
res, err := client.CreatePaste(paste)
want := regexp.MustCompile(`https://pastebin.com/([a-zA-Z0-9_]*)`)
if !want.MatchString(res) || err != nil {
t.Fatalf(`client.CreatePaste(paste) = %q, %v, want match for %#q, nil`, res, err, want)
}
}
func TestGetPastes(t *testing.T) {
res, err := client.GetPastes()
if len(res) == 0 || err != nil {
t.Fatalf(`client.GetPastes size = %q, %v, want != 0, nil`, len(res), err)
}
}
func TestGetRawPaste(t *testing.T) {
paste := model.FullPaste("This is a paste", model.Public, "npm run", model.TEN_MINUTES, "bash")
res, err := client.CreatePaste(paste)
if err != nil {
log.Fatalln("Error creating paste ", err)
}
want := regexp.MustCompile(`https://pastebin.com/(?P<Key>[a-zA-Z0-9_]*)`)
matches := want.FindStringSubmatch(res)
index := want.SubexpIndex("Key")
raw, err := client.GetRawPaste(matches[index])
if raw != "npm run" || err != nil {
t.Fatalf(`client.GetRawPaste(%v) = %q, %v, want %#q, nil`, matches[index], raw, err, "npm run")
}
}