Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
73 lines (58 loc) · 1.79 KB
/
Copy pathmain_test.go
File metadata and controls
73 lines (58 loc) · 1.79 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
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSimple(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
qs := r.URL.Query()
fmt.Fprintf(w, "hello, %s", qs.Get("name"))
}))
defer ts.Close()
r, err := checkReflected(ts.URL + "?name=Mr%20Naughty")
t.Logf("params reflected: %#v", r)
if err != nil {
t.Fatalf("expected nil error from checkReflected(), have %s", err)
}
if len(r) != 1 {
t.Errorf("wanted length 1 for returned keys, have %d", len(r))
}
}
func TestAppend(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
qs := r.URL.Query()
fmt.Fprintf(w, "hello, %s", qs.Get("name"))
}))
defer ts.Close()
r, err := checkAppend(ts.URL+"?name=Mr%20Naughty", "name", "somerandomvalue")
if err != nil {
t.Fatalf("expected nil error from checkAppend(), have %s", err)
}
if !r {
t.Errorf("wanted checkAppend() to return true, but it didn't")
}
}
func TestRunOutputsFinding(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
qs := r.URL.Query()
fmt.Fprintf(w, "hello, %s", qs.Get("name"))
}))
defer ts.Close()
var stdout bytes.Buffer
var stderr bytes.Buffer
err := run(strings.NewReader(ts.URL+"?name=Mr%20Naughty\n"), &stdout, &stderr)
if err != nil {
t.Fatalf("expected nil error from run(), have %s", err)
}
got := stdout.String()
if !strings.Contains(got, "URL: "+ts.URL+"?name=Mr%20Naughty Param: name Unfiltered:") {
t.Fatalf("expected run() to write a finding, stdout=%q stderr=%q", got, stderr.String())
}
}