Skip to content
Permalink
Browse files

Add tests for the library

  • Loading branch information
deletescape committed Oct 18, 2019
1 parent cb15fb1 commit 4ec32f38a55f07acdee2fe0809db24476c295e7e
Showing with 233 additions and 1 deletion.
  1. +1 −1 dogbin/dogbin.go
  2. +223 −0 dogbin/dogbin_test.go
  3. +1 −0 go.mod
  4. +8 −0 go.sum
@@ -145,7 +145,7 @@ func (d Server) Get(slug string) (*Document, error) {
if err = json.NewDecoder(r.Body).Decode(wrapper); err != nil {
return nil, fmt.Errorf("unable to decode response: %w", err)
}
if wrapper == nil {
if wrapper == nil || (wrapper.Content == "" && wrapper.Slug == "" && wrapper.Document == nil) {
return nil, errors.New("unable to decode response: document is empty")
}

@@ -0,0 +1,223 @@
package dogbin

import (
"encoding/json"
"flag"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var serverUrl string

func handler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
slug := r.URL.Path[len("/documents/"):]
switch slug {
case "exists":
io.WriteString(w, `{"data":"works","document":{"_id":"exists","content":"works","isUrl":false,"owner":{"$oid":"5b20334e5e7034132c431e78"},"version":2,"viewCount":12},"key":"exists"}`)
case "existshaste":
io.WriteString(w, `{"data":"works", "key":"existshaste"}`)
case "broken":
io.WriteString(w, "whoops")
case "broken2":
io.WriteString(w, "{}")
case "notexist":
w.WriteHeader(404)
io.WriteString(w, `{"message":"Document not found."}`)
case "whoops":
w.WriteHeader(500)
case "whoops2":
w.WriteHeader(500)
io.WriteString(w, "Internal Server Error")
case "whoops3":
w.WriteHeader(500)
io.WriteString(w, "{}")
default:
println(slug)
w.WriteHeader(404)
io.WriteString(w, `{"message":"Document not found."}`)
}
} else {
content := new(UploadRequest)
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(content)
if err != nil {
io.WriteString(w, `{"key":"hasteup"}`)
} else {
switch content.Slug {
case "works":
io.WriteString(w, `{"key":"works", "isUrl": false}`)
case "url":
io.WriteString(w, `{"key":"url", "isUrl": true}`)
case "duplicate":
w.WriteHeader(409)
io.WriteString(w, `{"message":"This URL is already in use, please choose a different one"}`)
}
}
}
}

var local Server

func TestMain(m *testing.M) {
flag.Parse()
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()
serverUrl = ts.URL
local = NewServer(ts.URL)
os.Exit(m.Run())
}

func TestNewServer(t *testing.T) {
assert := assert.New(t)

custom := NewServer("http://test")
assert.Equal("http://test", custom.server)
base, err := custom.baseUrl()
assert.Nil(err)
assert.Equal("http://test", base)

customPort := NewServer("http://test:90")
assert.Equal("http://test:90", customPort.server)
base, err = customPort.baseUrl()
assert.Nil(err)
assert.Equal("http://test:90", base)

dogbin := Dogbin()
assert.Equal("del.dog", dogbin.server)
base, err = dogbin.baseUrl()
assert.Nil(err)
assert.Equal("https://del.dog", base)

haste := Hastebin()
assert.Equal("hastebin.com", haste.server)
}

func TestGetExists(t *testing.T) {
assert := assert.New(t)

got, err := local.Get("exists")
require.Nil(t, err)

assert.Equal("exists", got.Slug, "Slug didn't match")
assert.Equal("works", got.Content, "Content didn't match")
assert.Equal(12, got.ViewCount, "ViewCount didn't match")
assert.Equal(false, got.IsUrl, "IsUrl didn't match")
}

func TestGetExistsHaste(t *testing.T) {
assert := assert.New(t)

got, err := local.Get("existshaste")
require.Nil(t, err)

assert.Equal("existshaste", got.Slug, "Slug didn't match")
assert.Equal("works", got.Content, "Content didn't match")
assert.Equal(0, got.ViewCount, "ViewCount should always be zero for haste servers")
assert.Equal(false, got.IsUrl, "IsUrl should always be false for haste servers")
}

func TestGetNotExists(t *testing.T) {
got, err := local.Get("notexist")
require.NotNil(t, err)

assert.Equal(t, "unable to make request: Document not found.", err.Error())
assert.Nil(t, got)
}

func TestGetBrokenResponse(t *testing.T) {
got, err := local.Get("broken")
require.NotNil(t, err)

assert.Equal(t, "unable to decode response: invalid character 'w' looking for beginning of value", err.Error())
assert.Nil(t, got)
}

func TestGetBrokenResponseValidJson(t *testing.T) {
got, err := local.Get("broken2")
require.NotNil(t, err)

assert.Equal(t, "unable to decode response: document is empty", err.Error())
assert.Nil(t, got)
}

func TestServerErrorEmpty(t *testing.T) {
got, err := local.Get("whoops")
require.NotNil(t, err)

assert.Equal(t, "unable to make request (500 Internal Server Error) and decode response: EOF", err.Error())
assert.Nil(t, got)
}

func TestServerError(t *testing.T) {
got, err := local.Get("whoops2")
require.NotNil(t, err)

assert.Equal(t, "unable to make request (500 Internal Server Error) and decode response: invalid character 'I' looking for beginning of value", err.Error())
assert.Nil(t, got)
}

func TestServerErrorValidJson(t *testing.T) {
got, err := local.Get("whoops3")
require.NotNil(t, err)

assert.Equal(t, "unable to make request: 500 Internal Server Error", err.Error())
assert.Nil(t, got)
}

func TestPutWorks(t *testing.T) {
assert := assert.New(t)
got, err := local.Put("works", "random content")

require.Nil(t, err)

assert.Equal("works", got.Slug, "Slug didn't match")
assert.Equal(false, got.IsUrl, "IsUrl didn't match")
assert.Equal(serverUrl+"/works", got.Url, "Url didn't match")
}

func TestPutUrlWorks(t *testing.T) {
assert := assert.New(t)
got, err := local.Put("url", "https://github.com/dogbin/inu")

require.Nil(t, err)

assert.Equal("url", got.Slug, "Slug didn't match")
assert.Equal(true, got.IsUrl, "IsUrl didn't match")
assert.Equal(serverUrl+"/url", got.Url, "Url didn't match")
}

func TestPutLegacyWorks(t *testing.T) {
assert := assert.New(t)
got, err := local.Put("", "random content")

require.Nil(t, err)

assert.Equal("hasteup", got.Slug, "Slug didn't match")
assert.Equal(false, got.IsUrl, "IsUrl didn't match")
assert.Equal(serverUrl+"/hasteup", got.Url, "Url didn't match")
}

func TestPutDuplicate(t *testing.T) {
got, err := local.Put("duplicate", "random content")

require.NotNil(t, err)

assert.Equal(t, "This URL is already in use, please choose a different one", err.Error())
assert.Nil(t, got)
}

func TestPutEmpty(t *testing.T) {
got, err := local.Put("empty", "")

require.NotNil(t, err)

assert.Equal(t, "no content was provided", err.Error())
assert.Nil(t, got)
}
1 go.mod
@@ -4,5 +4,6 @@ go 1.13

require (
github.com/atotto/clipboard v0.1.2
github.com/stretchr/testify v1.4.0
github.com/urfave/cli v1.22.1
)
8 go.sum
@@ -3,13 +3,21 @@ github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzb
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 4ec32f3

Please sign in to comment.
You can’t perform that action at this time.