2016-08-03 03:11:52 -04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2017-12-29 17:44:45 -05:00
|
|
|
"fmt"
|
2016-08-03 03:11:52 -04:00
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-15 00:57:36 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-08-05 01:47:04 -04:00
|
|
|
|
2016-08-16 21:42:08 -04:00
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
2016-08-03 03:11:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteError(t *testing.T) {
|
2022-01-15 14:01:23 -05:00
|
|
|
table := []struct {
|
2016-08-03 03:11:52 -04:00
|
|
|
reason, expected string
|
|
|
|
}{
|
|
|
|
{"hello world", "d14:failure reason11:hello worlde"},
|
|
|
|
{"what's up", "d14:failure reason9:what's upe"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range table {
|
2017-12-29 17:44:45 -05:00
|
|
|
t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
err := WriteError(r, bittorrent.ClientError(tt.reason))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, r.Body.String(), tt.expected)
|
|
|
|
})
|
2016-08-03 03:11:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteStatus(t *testing.T) {
|
2022-01-15 14:01:23 -05:00
|
|
|
table := []struct {
|
2017-12-29 17:44:45 -05:00
|
|
|
reason, expected string
|
|
|
|
}{
|
|
|
|
{"something is missing", "d14:failure reason20:something is missinge"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range table {
|
|
|
|
t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
err := WriteError(r, bittorrent.ClientError(tt.reason))
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, r.Body.String(), tt.expected)
|
|
|
|
})
|
|
|
|
}
|
2016-08-03 03:11:52 -04:00
|
|
|
}
|