-
Notifications
You must be signed in to change notification settings - Fork 0
/
drisqus_common_test.go
104 lines (84 loc) · 1.86 KB
/
drisqus_common_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright Piero de Salvia.
// All Rights Reserved
package drisqus
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"testing"
"github.com/pierods/gisqus"
"github.com/pierods/gisqus/mock"
)
var mockServer *mock.Server
var testErr error
var testGisqus gisqus.Gisqus
var testDrisqus Drisqus
var testCtx context.Context
var testValues url.Values
var testDataDir string
var runLiveTests = flag.Bool("live", false, "whether to run tests requiring a connection to the internet")
func init() {
testCtx, _ = context.WithCancel(context.TODO())
goPath := os.Getenv("GOPATH")
testDataDir = goPath + "/src/github.com/pierods/drisqus/testdata/"
}
func TestMain(m *testing.M) {
if !*runLiveTests {
testGisqus = gisqus.NewGisqus("secret")
testDrisqus = NewDrisqus(testGisqus)
mockServer = mock.NewMockServer()
defer mockServer.Close()
mockForumURLS()
mockThreadURLS()
mockPostURLS()
mockUserURLs()
} else {
key, err := readKeyFile()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
testGisqus = gisqus.NewGisqus(key)
testDrisqus = NewDrisqus(testGisqus)
// TODO live tests
}
retCode := m.Run()
os.Exit(retCode)
}
func switchHS(URL, JSON string) string {
result, err := mockServer.SwitchHostAndScheme(URL, JSON)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return result
}
func readTestFile(fileName string) string {
f, err := os.Open(testDataDir + fileName)
defer f.Close()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return string(bytes)
}
func readKeyFile() (string, error) {
f, err := os.Open(os.Getenv("GOPATH") + "/src/github.com/pierods/drisqus/demo/disqus_secret_key.txt")
defer f.Close()
if err != nil {
return "", err
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
return string(bytes), nil
}