server.Prime and server.serveAnnounce test
This commit is contained in:
parent
58586e6718
commit
cb99fce6c0
3 changed files with 94 additions and 0 deletions
|
@ -62,6 +62,7 @@ func newAnnounce(r *http.Request, conf *config.Config) (*announce, error) {
|
||||||
Downloaded: downloaded,
|
Downloaded: downloaded,
|
||||||
Event: event,
|
Event: event,
|
||||||
IP: ip,
|
IP: ip,
|
||||||
|
Infohash: infohash,
|
||||||
Left: left,
|
Left: left,
|
||||||
NumWant: numWant,
|
NumWant: numWant,
|
||||||
Passkey: passkey,
|
Passkey: passkey,
|
||||||
|
@ -96,6 +97,10 @@ func requestedIP(r *http.Request, pq *parsedQuery) (string, error) {
|
||||||
return string(xRealIPs[0]), nil
|
return string(xRealIPs[0]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.RemoteAddr == "" {
|
||||||
|
return "127.0.0.1", nil
|
||||||
|
}
|
||||||
|
|
||||||
portIndex := len(r.RemoteAddr) - 1
|
portIndex := len(r.RemoteAddr) - 1
|
||||||
for ; portIndex >= 0; portIndex-- {
|
for ; portIndex >= 0; portIndex-- {
|
||||||
if r.RemoteAddr[portIndex] == ':' {
|
if r.RemoteAddr[portIndex] == ':' {
|
||||||
|
|
18
server/primer.go
Normal file
18
server/primer.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by the BSD 2-Clause license,
|
||||||
|
// which can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/chihaya/chihaya/storage/backend"
|
||||||
|
"github.com/chihaya/chihaya/storage/tracker"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Primer represents a function that can prime storage with data.
|
||||||
|
type Primer func(tracker.Pool, backend.Conn) error
|
||||||
|
|
||||||
|
// Prime executes a priming function on the server.
|
||||||
|
func (s *Server) Prime(p Primer) error {
|
||||||
|
return p(s.trackerPool, s.backendConn)
|
||||||
|
}
|
71
server/serve_announce_test.go
Normal file
71
server/serve_announce_test.go
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by the BSD 2-Clause license,
|
||||||
|
// which can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/chihaya/chihaya/storage"
|
||||||
|
"github.com/chihaya/chihaya/storage/backend"
|
||||||
|
"github.com/chihaya/chihaya/storage/tracker"
|
||||||
|
|
||||||
|
_ "github.com/chihaya/chihaya/storage/backend/mock"
|
||||||
|
_ "github.com/chihaya/chihaya/storage/tracker/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAnnounce(t *testing.T) {
|
||||||
|
s, err := newTestServer()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Prime(func(t tracker.Pool, b backend.Conn) (err error) {
|
||||||
|
conn, err := t.Get()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.AddUser(&storage.User{
|
||||||
|
ID: 1,
|
||||||
|
Passkey: "yby47f04riwpndba456rqxtmifenq5h6",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.WhitelistClient("TR2820")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.AddTorrent(&storage.Torrent{
|
||||||
|
ID: 1,
|
||||||
|
Infohash: string([]byte{0x89, 0xd4, 0xbc, 0x52, 0x11, 0x16, 0xca, 0x1d, 0x42, 0xa2, 0xf3, 0x0d, 0x1f, 0x27, 0x4d, 0x94, 0xe4, 0x68, 0x1d, 0xaf}),
|
||||||
|
Seeders: make(map[string]storage.Peer),
|
||||||
|
Leechers: make(map[string]storage.Peer),
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
url := "http://localhost:34000/yby47f04riwpndba456rqxtmifenq5h6/announce?info_hash=%89%d4%bcR%11%16%ca%1dB%a2%f3%0d%1f%27M%94%e4h%1d%af&peer_id=-TR2820-l71jtqkl898b&port=51413&uploaded=0&downloaded=0&left=0&numwant=0&key=3c8e3319&compact=1&supportcrypto=1&event=stopped"
|
||||||
|
r, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.serveAnnounce(w, r)
|
||||||
|
|
||||||
|
if w.Body.String() != "1:d8:completei0e10:incompletei1e8:intervali1800e12:min intervali900e1:e" {
|
||||||
|
t.Errorf("improper response from server")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue