2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-05-29 19:50:52 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-29 15:59:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mux *http.ServeMux
|
|
|
|
ctx = context.Background()
|
|
|
|
client *Client
|
|
|
|
server *httptest.Server
|
|
|
|
)
|
|
|
|
|
|
|
|
func setup() {
|
|
|
|
mux = http.NewServeMux()
|
|
|
|
server = httptest.NewServer(mux)
|
|
|
|
|
|
|
|
mux.HandleFunc("/api/v1/access_token", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
response := `
|
|
|
|
{
|
|
|
|
"access_token": "token1",
|
|
|
|
"token_type": "bearer",
|
|
|
|
"expires_in": 3600,
|
|
|
|
"scope": "*"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
w.Header().Add(headerContentType, mediaTypeJSON)
|
|
|
|
fmt.Fprint(w, response)
|
|
|
|
})
|
|
|
|
|
|
|
|
client, _ = NewClient(nil,
|
|
|
|
WithOAuth2("id1", "secret1", "user1", "password1"),
|
|
|
|
WithBaseURL(server.URL),
|
|
|
|
WithTokenURL(server.URL+"/api/v1/access_token"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func teardown() {
|
|
|
|
server.Close()
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:56:32 -04:00
|
|
|
func readFileContents(path string) (string, error) {
|
|
|
|
file, err := os.Open(path)
|
2020-04-29 15:59:18 -04:00
|
|
|
if err != nil {
|
2020-07-21 21:56:32 -04:00
|
|
|
return "", err
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2020-07-21 21:56:32 -04:00
|
|
|
return "", err
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 21:56:32 -04:00
|
|
|
return string(bytes), err
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testClientServices(t *testing.T, c *Client) {
|
|
|
|
services := []string{
|
2020-06-27 23:53:59 -04:00
|
|
|
"Account",
|
2020-04-29 15:59:18 -04:00
|
|
|
"Comment",
|
|
|
|
"Flair",
|
|
|
|
"Listings",
|
2020-07-13 23:05:24 -04:00
|
|
|
"Moderation",
|
|
|
|
"Multi",
|
2020-06-22 21:52:34 -04:00
|
|
|
"Post",
|
2020-06-27 23:53:59 -04:00
|
|
|
"Search",
|
2020-04-29 15:59:18 -04:00
|
|
|
"Subreddit",
|
|
|
|
"User",
|
|
|
|
}
|
|
|
|
|
|
|
|
cp := reflect.ValueOf(c)
|
|
|
|
cv := reflect.Indirect(cp)
|
|
|
|
|
|
|
|
for _, s := range services {
|
|
|
|
if cv.FieldByName(s).IsNil() {
|
|
|
|
t.Fatalf("c.%s shouldn't be nil", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testClientDefaultUserAgent(t *testing.T, c *Client) {
|
|
|
|
if expect, actual := fmt.Sprintf("golang:%s:v%s (by /u/)", libraryName, libraryVersion), c.userAgent; expect != actual {
|
|
|
|
t.Fatalf("got unexpected value\nexpect: %+v\nactual: %+v", Stringify(expect), Stringify(actual))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testClientDefaults(t *testing.T, c *Client) {
|
|
|
|
testClientDefaultUserAgent(t, c)
|
|
|
|
testClientServices(t, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
|
|
c, err := NewClient(nil)
|
2020-05-29 19:50:52 -04:00
|
|
|
assert.NoError(t, err)
|
2020-04-29 15:59:18 -04:00
|
|
|
testClientDefaults(t, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_Error(t *testing.T) {
|
|
|
|
errorOpt := func(c *Client) error {
|
|
|
|
return errors.New("foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := NewClient(nil, errorOpt)
|
2020-05-29 19:50:52 -04:00
|
|
|
assert.EqualError(t, err, "foo")
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|