diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go
index d463425..d360806 100644
--- a/plugins/nerdepedia/nerdepedia.go
+++ b/plugins/nerdepedia/nerdepedia.go
@@ -21,6 +21,18 @@ const (
closingTagSuffix = "\" />"
)
+type HTTPClient interface {
+ Do(req *http.Request) (*http.Response, error)
+}
+
+var (
+ client HTTPClient
+)
+
+func init() {
+ client = &http.Client{}
+}
+
type NerdepediaPlugin struct {
bot bot.Bot
config *config.Config
@@ -37,26 +49,50 @@ func New(b bot.Bot) *NerdepediaPlugin {
return np
}
+func defaultSites() map[string]string {
+ starWars := "http://starwars.wikia.com/wiki/Special:Random"
+ starTrek := "http://memory-alpha.wikia.com/wiki/Special:Random"
+ dune := "http://dune.wikia.com/wiki/Special:Random"
+ lotr := "http://lotr.wikia.com/wiki/Special:Random"
+ pokemon := "http://pokemon.wikia.com/wiki/Special:Random"
+
+ return map[string]string{
+ "may the force be with you": starWars,
+ "help me obi-wan": starWars,
+
+ "beam me up scotty": starTrek,
+ "live long and prosper": starTrek,
+
+ "bless the maker": dune,
+ "i must not fear": dune,
+ "the spice must flow": dune,
+
+ "my precious": lotr,
+ "one ring to rule them all": lotr,
+ "one does not simply walk into mordor": lotr,
+
+ "pikachu i choose you": pokemon,
+ "gotta catch em all": pokemon,
+ }
+}
+
// Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *NerdepediaPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
lowerCase := strings.ToLower(message.Body)
query := ""
- if lowerCase == "may the force be with you" || lowerCase == "help me obi-wan" {
- query = "http://starwars.wikia.com/wiki/Special:Random"
- } else if lowerCase == "beam me up scotty" || lowerCase == "live long and prosper" {
- query = "http://memory-alpha.wikia.com/wiki/Special:Random"
- } else if lowerCase == "bless the maker" || lowerCase == "i must not fear" || lowerCase == "the spice must flow" {
- query = "http://dune.wikia.com/wiki/Special:Random"
- } else if lowerCase == "my precious" || lowerCase == "one ring to rule them all" || lowerCase == "one does not simply walk into mordor" {
- query = "http://lotr.wikia.com/wiki/Special:Random"
- } else if lowerCase == "pikachu i choose you" || lowerCase == "gotta catch em all" {
- query = "http://pokemon.wikia.com/wiki/Special:Random"
+ queries := p.config.GetMap("nerdepedia.sites", defaultSites())
+ for k, v := range queries {
+ if lowerCase == k {
+ query = v
+ break
+ }
}
if query != "" {
- resp, err := http.Get(query)
+ req, _ := http.NewRequest(http.MethodGet, query, nil)
+ resp, err := client.Do(req)
if err != nil {
return false
}
diff --git a/plugins/nerdepedia/nerdepeida_test.go b/plugins/nerdepedia/nerdepeida_test.go
index a135a31..9e759af 100644
--- a/plugins/nerdepedia/nerdepeida_test.go
+++ b/plugins/nerdepedia/nerdepeida_test.go
@@ -3,16 +3,42 @@
package nerdepedia
import (
- "github.com/velour/catbase/plugins/cli"
+ "bytes"
+ "io"
+ "io/ioutil"
+ "net/http"
"strings"
"testing"
+ "github.com/rs/zerolog/log"
+
+ "github.com/velour/catbase/plugins/cli"
+
"github.com/stretchr/testify/assert"
+
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
)
+var body = []byte(`
+
+`)
+
+type MockClient struct {
+ Status int
+ Body io.ReadCloser
+ Err error
+}
+
+func (cl MockClient) Do(req *http.Request) (*http.Response, error) {
+ log.Debug().Msgf("Returning mock response")
+ return &http.Response{
+ StatusCode: cl.Status,
+ Body: cl.Body,
+ }, cl.Err
+}
+
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
@@ -30,34 +56,12 @@ func TestWars(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
+ client = MockClient{
+ Status: http.StatusOK,
+ Body: ioutil.NopCloser(bytes.NewReader(body)),
+ Err: nil,
+ }
res := c.message(makeMessage("help me obi-wan"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
}
-
-func TestTrek(t *testing.T) {
- mb := bot.NewMockBot()
- c := New(mb)
- assert.NotNil(t, c)
- res := c.message(makeMessage("live long and prosper"))
- assert.Len(t, mb.Messages, 1)
- assert.True(t, res)
-}
-
-func TestDune(t *testing.T) {
- mb := bot.NewMockBot()
- c := New(mb)
- assert.NotNil(t, c)
- res := c.message(makeMessage("bless the maker"))
- assert.Len(t, mb.Messages, 1)
- assert.True(t, res)
-}
-
-func TestPoke(t *testing.T) {
- mb := bot.NewMockBot()
- c := New(mb)
- assert.NotNil(t, c)
- res := c.message(makeMessage("gotta catch em all"))
- assert.Len(t, mb.Messages, 1)
- assert.True(t, res)
-}