forked from FeNoMeNa/cwmp-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_proxy_test.go
91 lines (71 loc) · 2.01 KB
/
e2e_proxy_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
package main
import (
"bytes"
"io"
"net/http"
"testing"
)
func TestProxyServer(t *testing.T) {
c := struct {
username, password string
cpeWokenup bool
}{
"FeNoMeNa", "12345678", false,
}
cpe := fakeHttpServer(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok {
w.Header().Add("WWW-Authenticate", `Basic realm="wakeup-test"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
if c.username != username {
t.Errorf("expected username: %v", c.username)
t.Errorf(" got username: %v", username)
}
if c.password != password {
t.Errorf("expected password: %v", c.password)
t.Errorf(" got password: %v", password)
}
c.cpeWokenup = true
})
in := bytes.NewReader([]byte(
`
<ParameterValueStruct>
<Name>InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name>
<Value xsi:type="xsd:string">` + cpe.URL + `</Value>
</ParameterValueStruct>
`,
))
want := bytes.NewReader([]byte(
`
<ParameterValueStruct>
<Name>InternetGatewayDevice.ManagementServer.ConnectionRequestURL</Name>
<Value xsi:type="xsd:string">http://localhost:1717/client?origin=` + cpe.URL + `</Value>
</ParameterValueStruct>
`,
))
backend := fakeHttpServer(func(w http.ResponseWriter, r *http.Request) {
compareReaders(t, want, r.Body)
url := "http://localhost:1717/client?origin=" + cpe.URL
sendWakeupRequest(url, c.username, c.password)
})
// Let's hope this port to be free
startProxy(1717, backend.URL)
sendInform("http://localhost:1717", in)
if c.cpeWokenup == false {
t.Errorf("The CPE is not woken up!")
}
}
func startProxy(port int, backend string) {
proxy, _ := NewProxy(port, backend)
go proxy.Start() // Run proxy server in background
}
func sendInform(url string, inform io.Reader) {
http.Post(url, "text/xml", inform)
}
func sendWakeupRequest(url, username, password string) {
request, _ := http.NewRequest("GET", url, nil)
request.SetBasicAuth(username, password)
http.DefaultClient.Do(request)
}