| 1 |
package transport |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/base64" |
| 6 |
"io/ioutil" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/mitch000001/go-hbci/transport" |
| 12 |
|
| 13 |
"golang.org/x/text/encoding" |
| 14 |
"golang.org/x/text/transform" |
| 15 |
) |
| 16 |
|
| 17 |
type mockEncoding struct { |
| 18 |
encoder *encoding.Encoder |
| 19 |
decoder *encoding.Decoder |
| 20 |
} |
| 21 |
|
| 22 |
func (m mockEncoding) NewEncoder() *encoding.Encoder { |
| 23 |
return m.encoder |
| 24 |
} |
| 25 |
|
| 26 |
func (m mockEncoding) NewDecoder() *encoding.Decoder { |
| 27 |
return m.decoder |
| 28 |
} |
| 29 |
|
| 30 |
type mockTransform struct { |
| 31 |
transform.Transformer |
| 32 |
transformCallCount int |
| 33 |
resetCallCount int |
| 34 |
} |
| 35 |
|
| 36 |
func (m *mockTransform) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { |
| 37 |
m.transformCallCount++ |
| 38 |
buf := bytes.ToUpper(src) |
| 39 |
return m.Transformer.Transform(dst, buf, atEOF) |
| 40 |
} |
| 41 |
|
| 42 |
func (m *mockTransform) Reset() { |
| 43 |
m.resetCallCount++ |
| 44 |
m.Transformer.Reset() |
| 45 |
} |
| 46 |
|
| 47 |
func TestUTF8Encoding(t *testing.T) { |
| 48 |
called := false |
| 49 |
transportResponse := transport.Response{Body: ioutil.NopCloser(strings.NewReader("qux"))} |
| 50 |
var transportRequest *transport.Request |
| 51 |
innerTransport := transport.Func(func(req *transport.Request) (*transport.Response, error) { |
| 52 |
called = true |
| 53 |
transportRequest = req |
| 54 |
response := transportResponse |
| 55 |
return &response, nil |
| 56 |
}) |
| 57 |
encoderTransform := mockTransform{Transformer: encoding.Nop.NewEncoder().Transformer} |
| 58 |
decoderTransform := mockTransform{Transformer: encoding.Nop.NewDecoder().Transformer} |
| 59 |
encoding := mockEncoding{ |
| 60 |
encoder: &encoding.Encoder{ |
| 61 |
Transformer: &encoderTransform, |
| 62 |
}, |
| 63 |
decoder: &encoding.Decoder{ |
| 64 |
Transformer: &decoderTransform, |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
middleware := UTF8Encoding(encoding) |
| 69 |
|
| 70 |
wrappedTransport := middleware(innerTransport) |
| 71 |
|
| 72 |
request := &transport.Request{ |
| 73 |
URL: "foo", |
| 74 |
Body: ioutil.NopCloser(strings.NewReader("bar")), |
| 75 |
} |
| 76 |
|
| 77 |
response, err := wrappedTransport.Do(request) |
| 78 |
|
| 79 |
{ |
| 80 |
actual := called |
| 81 |
expected := true |
| 82 |
if expected != actual { |
| 83 |
t.Logf("Expected transform to be called, was not") |
| 84 |
t.Fail() |
| 85 |
} |
| 86 |
} |
| 87 |
{ |
| 88 |
actual, err := ioutil.ReadAll(response.Body) |
| 89 |
if err != nil { |
| 90 |
panic(err) |
| 91 |
} |
| 92 |
expected := []byte("QUX") |
| 93 |
if !reflect.DeepEqual(expected, actual) { |
| 94 |
t.Logf("Expected response Body to equal\n%+#v\n\tgot\n%+#v\n", expected, actual) |
| 95 |
t.Fail() |
| 96 |
} |
| 97 |
} |
| 98 |
{ |
| 99 |
actual, err := ioutil.ReadAll(transportRequest.Body) |
| 100 |
if err != nil { |
| 101 |
panic(err) |
| 102 |
} |
| 103 |
expected := []byte("BAR") |
| 104 |
if !reflect.DeepEqual(expected, actual) { |
| 105 |
t.Logf("Expected transport request body to equal\n%+#v\n\tgot\n%+#v\n", expected, actual) |
| 106 |
t.Fail() |
| 107 |
} |
| 108 |
} |
| 109 |
{ |
| 110 |
if err != nil { |
| 111 |
t.Logf("Expected transport err to equal nil, got\n%+#v\n", err) |
| 112 |
t.Fail() |
| 113 |
} |
| 114 |
} |
| 115 |
{ |
| 116 |
actual := encoderTransform.transformCallCount |
| 117 |
expected := 1 |
| 118 |
if expected != actual { |
| 119 |
t.Logf("Expected encoder transform to be called once, was %d", actual) |
| 120 |
t.Fail() |
| 121 |
} |
| 122 |
} |
| 123 |
{ |
| 124 |
actual := decoderTransform.transformCallCount |
| 125 |
expected := 2 |
| 126 |
if expected != actual { |
| 127 |
t.Logf("Expected decoder transform to be called once, was %d", actual) |
| 128 |
t.Fail() |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func mustDecode(res []byte, err error) []byte { |
| 134 |
if err != nil { |
| 135 |
panic(err) |
| 136 |
} |
| 137 |
return res |
| 138 |
} |
| 139 |
|
| 140 |
func TestBase64Encoding(t *testing.T) { |
| 141 |
encoding := base64.StdEncoding |
| 142 |
|
| 143 |
called := false |
| 144 |
transportResponse := transport.Response{ |
| 145 |
Body: ioutil.NopCloser(strings.NewReader(encoding.EncodeToString([]byte("qux")))), |
| 146 |
} |
| 147 |
var transportRequest *transport.Request |
| 148 |
innerTransport := transport.Func(func(req *transport.Request) (*transport.Response, error) { |
| 149 |
called = true |
| 150 |
transportRequest = req |
| 151 |
response := transportResponse |
| 152 |
return &response, nil |
| 153 |
}) |
| 154 |
|
| 155 |
middleware := Base64Encoding(encoding) |
| 156 |
|
| 157 |
wrappedTransport := middleware(innerTransport) |
| 158 |
|
| 159 |
request := &transport.Request{ |
| 160 |
URL: "foo", |
| 161 |
Body: ioutil.NopCloser(strings.NewReader("bar")), |
| 162 |
} |
| 163 |
|
| 164 |
response, _ := wrappedTransport.Do(request) |
| 165 |
|
| 166 |
{ |
| 167 |
actual := called |
| 168 |
expected := true |
| 169 |
if expected != actual { |
| 170 |
t.Logf("Expected transform to be called, was not") |
| 171 |
t.Fail() |
| 172 |
} |
| 173 |
} |
| 174 |
{ |
| 175 |
actual, err := ioutil.ReadAll(response.Body) |
| 176 |
if err != nil { |
| 177 |
panic(err) |
| 178 |
} |
| 179 |
expected := []byte("qux") |
| 180 |
if !reflect.DeepEqual(expected, actual) { |
| 181 |
t.Logf("Expected response Body to equal\n%s\n\tgot\n%s\n", expected, actual) |
| 182 |
t.Fail() |
| 183 |
} |
| 184 |
} |
| 185 |
{ |
| 186 |
actual, err := ioutil.ReadAll(transportRequest.Body) |
| 187 |
if err != nil { |
| 188 |
panic(err) |
| 189 |
} |
| 190 |
expected := []byte(encoding.EncodeToString([]byte("bar"))) |
| 191 |
if !reflect.DeepEqual(expected, actual) { |
| 192 |
t.Logf("Expected transport request body to equal\n%s\n\tgot\n%s\n", expected, actual) |
| 193 |
t.Fail() |
| 194 |
} |
| 195 |
} |
| 196 |
} |