| 1 |
package transport |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/base64" |
| 6 |
"io/ioutil" |
| 7 |
"net/http" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/mitch000001/go-hbci/transport" |
| 12 |
middleware "github.com/mitch000001/go-hbci/transport/middleware" |
| 13 |
) |
| 14 |
|
| 15 |
func TestHttpsBase64Transport(t *testing.T) { |
| 16 |
response := []byte("HNHBK:1:3+abc'HNDGC:2:3+def'") |
| 17 |
roundtripper := &MockHTTPTransport{} |
| 18 |
roundtripper.SetResponsePayloads([][]byte{ |
| 19 |
response, |
| 20 |
response, |
| 21 |
}) |
| 22 |
httpClient := &http.Client{Transport: roundtripper} |
| 23 |
|
| 24 |
httpsTransport := middleware.Base64Encoding(base64.StdEncoding)(&HTTPSTransport{httpClient}) |
| 25 |
httpsBase64Transport := &HTTPSBase64Transport{httpClient} |
| 26 |
|
| 27 |
httpReq := &transport.Request{ |
| 28 |
URL: "foo", |
| 29 |
Body: ioutil.NopCloser(strings.NewReader("bar")), |
| 30 |
} |
| 31 |
|
| 32 |
httpBase64Req := &transport.Request{ |
| 33 |
URL: "foo", |
| 34 |
Body: ioutil.NopCloser(strings.NewReader("bar")), |
| 35 |
} |
| 36 |
|
| 37 |
httpResponse, httpError := httpsTransport.Do(httpReq) |
| 38 |
httpBase64Respose, httpBase64Error := httpsBase64Transport.Do(httpBase64Req) |
| 39 |
|
| 40 |
if httpError != nil { |
| 41 |
t.Logf("HTTP: Expected no error, got %v\n", httpError) |
| 42 |
t.Fail() |
| 43 |
} |
| 44 |
|
| 45 |
if httpBase64Error != nil { |
| 46 |
t.Logf("HTTPBase64: Expected no error, got %v\n", httpBase64Error) |
| 47 |
t.Fail() |
| 48 |
} |
| 49 |
|
| 50 |
if httpResponse == nil { |
| 51 |
t.Logf("HTTP: Expected response not to be nil\n") |
| 52 |
t.Fail() |
| 53 |
} |
| 54 |
|
| 55 |
if httpBase64Respose == nil { |
| 56 |
t.Logf("HTTBase64: Expected response not to be nil\n") |
| 57 |
t.Fail() |
| 58 |
} |
| 59 |
|
| 60 |
httpResponseBytes, err := ioutil.ReadAll(httpResponse.Body) |
| 61 |
if err != nil { |
| 62 |
panic(err) |
| 63 |
} |
| 64 |
httpBase64ResponseBytes, err := ioutil.ReadAll(httpBase64Respose.Body) |
| 65 |
if err != nil { |
| 66 |
panic(err) |
| 67 |
} |
| 68 |
|
| 69 |
if !bytes.Equal(httpBase64ResponseBytes, httpResponseBytes) { |
| 70 |
t.Logf("Expected response body to equal\n%q\n\tgot\n%q\n", httpBase64ResponseBytes, httpResponseBytes) |
| 71 |
t.Fail() |
| 72 |
} |
| 73 |
|
| 74 |
requests := roundtripper.Requests() |
| 75 |
|
| 76 |
if len(requests) != 2 { |
| 77 |
t.Logf("Expected 2 requests, got %d\n", len(requests)) |
| 78 |
t.FailNow() |
| 79 |
} |
| 80 |
|
| 81 |
if requests[0] == nil { |
| 82 |
t.Logf("HTTP: Expected request not to be nil\n") |
| 83 |
t.Fail() |
| 84 |
} |
| 85 |
|
| 86 |
if requests[1] == nil { |
| 87 |
t.Logf("HTTBase64: Expected request not to be nil\n") |
| 88 |
t.Fail() |
| 89 |
} |
| 90 |
|
| 91 |
httpRequest, err := ioutil.ReadAll(requests[0].Body) |
| 92 |
if err != nil { |
| 93 |
panic(err) |
| 94 |
} |
| 95 |
httpBase64Request, err := ioutil.ReadAll(requests[1].Body) |
| 96 |
if err != nil { |
| 97 |
panic(err) |
| 98 |
} |
| 99 |
|
| 100 |
if !bytes.Equal(httpBase64Request, httpRequest) { |
| 101 |
t.Logf("Expected request to equal\n%q\n\tgot\n%q\n", httpBase64Request, httpRequest) |
| 102 |
t.Fail() |
| 103 |
} |
| 104 |
} |