-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBTN_test.go
More file actions
90 lines (82 loc) · 2.49 KB
/
Copy pathBTN_test.go
File metadata and controls
90 lines (82 loc) · 2.49 KB
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
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestGetTorrentIdentifierIsStableAndCaseInsensitive(t *testing.T) {
id1 := GetTorrentIdentifier("ABCDEF0123456789")
id2 := GetTorrentIdentifier("abcdef0123456789")
if id1 != id2 {
t.Fatalf("GetTorrentIdentifier should be case-insensitive: %q != %q", id1, id2)
}
if len(id1) != 64 {
t.Fatalf("identifier length=%d, want 64", len(id1))
}
if id1 == "abcdef0123456789" {
t.Fatal("identifier should not equal the raw torrent hash")
}
}
func TestBTNGetConfigLoadsConfiguration(t *testing.T) {
oldClientExternal := httpClientExternal
oldConfig := *config
oldBtnConfig := btnConfig
oldLastGetConfig := btn_lastGetConfig
oldCurrentTimestamp := currentTimestamp
oldIsGetting := btn_isGettingConfig.Load()
defer func() {
httpClientExternal = oldClientExternal
tmpConf := oldConfig
config = &tmpConf
btnConfig = oldBtnConfig
btn_lastGetConfig = oldLastGetConfig
currentTimestamp = oldCurrentTimestamp
btn_isGettingConfig.Store(oldIsGetting)
}()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Authorization"); got != "Bearer app-a@secret-a" {
t.Fatalf("Authorization=%q, want Bearer app-a@secret-a", got)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
"min_protocol_version": 1,
"max_protocol_version": 3,
"ability": {
"submit_peers": {
"interval": 10,
"endpoint": "https://btn.example/submit_peers",
"random_initial_delay": 0,
"version": "v1"
}
}
}`))
}))
defer server.Close()
httpClientExternal = *server.Client()
tmpConf := oldConfig
config = &tmpConf
config.BTNConfigureURL = server.URL
config.BTNAppID = "app-a"
config.BTNAppSecret = "secret-a"
btnConfig = nil
btn_lastGetConfig = 0
currentTimestamp = 100
btn_isGettingConfig.Store(false)
BTN_GetConfig()
if btnConfig == nil {
t.Fatal("BTN_GetConfig should populate btnConfig")
}
if btnConfig.MinMainVersion != 1 || btnConfig.MaxMainVersion != 3 {
t.Fatalf("unexpected protocol range: min=%d max=%d", btnConfig.MinMainVersion, btnConfig.MaxMainVersion)
}
ability, ok := btnConfig.Ability["submit_peers"]
if !ok {
t.Fatal("submit_peers ability should exist")
}
if ability.Endpoint != "https://btn.example/submit_peers" {
t.Fatalf("Endpoint=%q, want https://btn.example/submit_peers", ability.Endpoint)
}
if btn_lastGetConfig != 100 {
t.Fatalf("btn_lastGetConfig=%d, want 100", btn_lastGetConfig)
}
}