-
Notifications
You must be signed in to change notification settings - Fork 20
/
http_test.go
114 lines (104 loc) · 2.66 KB
/
http_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
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
//"strings"
"sync"
"testing"
"time"
"github.com/espebra/filebin2/dbl"
"github.com/espebra/filebin2/ds"
"github.com/espebra/filebin2/geoip"
"github.com/espebra/filebin2/s3"
)
const (
testLimitFileDownloads = 2
testLimitStorage = 10000000
testExpiredAt = 5
testHTTPHost = "localhost"
testHTTPPort = 8080
testDbName = "db"
testDbUser = "username"
testDbPassword = "changeme"
testDbHost = "db"
testDbPort = 5432
testS3Endpoint = "s3:9000"
testS3Region = "us-east-1"
testS3Bucket = "testbin"
testS3AccessKey = "s3accesskey"
testS3SecretKey = "s3secretkey"
)
var (
waitForServer sync.WaitGroup
)
func tearUp() (dao dbl.DAO, s3ao s3.S3AO, err error) {
dao, err = dbl.Init(testDbHost, testDbPort, testDbName, testDbUser, testDbPassword)
if err != nil {
return dao, s3ao, err
}
if err := dao.ResetDB(); err != nil {
return dao, s3ao, err
}
expiry := time.Second * 10
s3ao, err = s3.Init(testS3Endpoint, testS3Bucket, testS3Region, testS3AccessKey, testS3SecretKey, false, expiry)
if err != nil {
return dao, s3ao, err
}
s3ao.SetTrace(true)
return dao, s3ao, nil
}
func tearDown(dao dbl.DAO) error {
if err := dao.ResetDB(); err != nil {
return err
}
err := dao.Close()
return err
}
func startHTTPServer(l net.Listener, wg *sync.WaitGroup, h http.Handler) {
server := &http.Server{Addr: l.Addr().String(), Handler: h}
wg.Done()
server.Serve(l)
}
func TestMain(m *testing.M) {
dao, s3ao, err := tearUp()
if err != nil {
log.Fatal(err)
}
c := ds.Config{
LimitFileDownloads: testLimitFileDownloads,
LimitStorageBytes: testLimitStorage,
Expiration: testExpiredAt,
HttpHost: testHTTPHost,
HttpPort: testHTTPPort,
RejectFileExtensions: []string{"illegal1", "illegal2"},
}
metrics := ds.Metrics{}
geodb, err := geoip.Init("mmdb/GeoLite2-ASN.mmdb", "mmdb/GeoLite2-City.mmdb")
if err != nil {
fmt.Printf("Unable to load geoip database: %s\n", err.Error())
os.Exit(2)
}
h := &HTTP{
staticBox: &staticBox,
templateBox: &templateBox,
dao: &dao,
s3: &s3ao,
geodb: &geodb,
config: &c,
metrics: &metrics,
}
if err := h.Init(); err != nil {
fmt.Printf("Unable to start the HTTP server: %s\n", err.Error())
os.Exit(2)
}
tcpListener, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", h.config.HttpHost, h.config.HttpPort))
waitForServer.Add(1)
go startHTTPServer(tcpListener, &waitForServer, h.router)
retCode := m.Run()
tcpListener.Close()
tearDown(dao)
os.Exit(retCode)
}