-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhash_test.go
71 lines (60 loc) · 2.03 KB
/
hash_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
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"io"
"net/http"
"os/exec"
"strings"
"testing"
)
type testCommander struct {
DefaultCommander
stdin *bytes.Buffer
}
func (c testCommander) NewCmd(command string, stderr io.Writer, stdin io.Reader) *exec.Cmd {
cmd := c.DefaultCommander.NewCmd(command, stderr, stdin)
// Copy the STDIN sent to "command" to our bytes.Buffer for inspection later
cmd.Stdin = io.TeeReader(cmd.Stdin, c.stdin)
return cmd
}
func TestDefaultHasherExcludesBody(t *testing.T) {
hasher := DefaultHasher{}
body := "HASH THIS BODY"
req, _ := http.NewRequest("POST", "/foobar", strings.NewReader(body))
req.Header.Set("chameleon-no-hash-body", "true")
hash := hasher.Hash(req)
md5Hasher := md5.New()
md5Hasher.Write([]byte(req.URL.RequestURI() + req.Method))
expected := hex.EncodeToString(md5Hasher.Sum(nil))
if hash != expected {
t.Errorf("Got: `%v`; Expected: `%v`", hash, expected)
}
}
func TestDefaultHasherIncludesBody(t *testing.T) {
hasher := DefaultHasher{}
body := "HASH THIS BODY"
reqWithHeader, _ := http.NewRequest("POST", "/foobar", strings.NewReader(body))
reqWithHeader.Header.Set("chameleon-hash-body", "true")
reqWithoutHeader, _ := http.NewRequest("POST", "/foobar", strings.NewReader(body))
withHeader := hasher.Hash(reqWithHeader)
withoutHeader := hasher.Hash(reqWithoutHeader)
if withoutHeader != withHeader {
t.Errorf("Request hashes do not match: `%v` != `%v`", withoutHeader, withHeader)
}
}
func TestCmdHasher(t *testing.T) {
var stdin bytes.Buffer
hasher := CmdHasher{Command: "/bin/cat", Commander: testCommander{stdin: &stdin}}
req, _ := http.NewRequest("POST", "/foobar", strings.NewReader("HASH THIS BODY"))
req.Header.Set("chameleon-hash-body", "true")
hash := hasher.Hash(req)
md5Hasher := md5.New()
// our command just echoes back what we gave it, so all of stdin should be included in the hash
md5Hasher.Write(stdin.Bytes())
expected := hex.EncodeToString(md5Hasher.Sum(nil))
if hash != expected {
t.Errorf("Got: `%v`; Expected: `%v`", hash, expected)
}
}