Skip to content

Commit 248aa85

Browse files
authored
tests(testutil): add tests for StripProgress and AssertBody (#231)
1 parent a330059 commit 248aa85

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

pkg/testutil/testutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ func AssertBody(t *testing.T, expected interface{}, req *http.Request) {
8787
// StripProgress strips the output from the progress method
8888
func StripProgress(input string) string {
8989
first := strings.Index(input, "\b")
90-
// If /b charecter not part of string
90+
// If \b charecter not part of string
9191
if first == -1 {
9292
return input
9393
}
9494
last := strings.LastIndex(input, "\b")
9595

96-
// return string without /b or the charecters it deletes.
96+
// return string without \b or the characters it deletes.
9797
return input[:first-(last-first+1)] + input[last+1:]
9898
}
9999

pkg/testutil/testutil_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package testutil
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/arschles/assert"
10+
)
11+
12+
type nopCloser struct {
13+
io.Reader
14+
}
15+
16+
func (nopCloser) Close() error { return nil }
17+
18+
// TestStripProgress ensures StripProgress strips what is expected.
19+
func TestStripProgress(t *testing.T) {
20+
testInput := "Lorem ipsum dolar sit amet"
21+
expectedOutput := "Lorem ipsum dolar sit amet"
22+
23+
assert.Equal(t, StripProgress(testInput), expectedOutput, "output")
24+
25+
testInput = "Lorem ipsum dolar sit amet...\b\b\b"
26+
assert.Equal(t, StripProgress(testInput), expectedOutput, "output")
27+
}
28+
29+
// TestAssertBody ensures AssertBody correctly marshals into the interface.
30+
func TestAssertBody(t *testing.T) {
31+
32+
b := nopCloser{bytes.NewBufferString(`{"data":{"lorem":"ipsum"},"dolar":["sit","amet"]}`)}
33+
34+
sampleRequest := http.Request{
35+
Body: b,
36+
}
37+
38+
expected := map[string]interface{}{
39+
"data": map[string]interface{}{
40+
"lorem": "ipsum",
41+
},
42+
"dolar": []string{
43+
"sit",
44+
"amet",
45+
},
46+
}
47+
48+
AssertBody(t, expected, &sampleRequest)
49+
}

0 commit comments

Comments
 (0)