Browse Source

fix tests

Christine Dodrill 7 months ago
parent
commit
2bdce02090
4 changed files with 30 additions and 10 deletions
  1. 2 3
      go.mod
  2. 2 0
      go.sum
  3. 25 6
      gopher.go
  4. 1 1
      gopher_test.go

+ 2 - 3
go.mod

@@ -1,8 +1,7 @@
-module github.com/prologic/go-gopher
+module within.website/gopher
 
 require (
-	github.com/davecgh/go-spew v1.1.1 // indirect
-	github.com/pmezard/go-difflib v1.0.0 // indirect
+	github.com/prologic/go-gopher v0.0.0-20181230133552-0c68ed5f58b0
 	github.com/stretchr/testify v1.2.2
 	golang.org/x/net v0.0.0-20181220203305-927f97764cc3
 	within.website/ln v0.6.0

+ 2 - 0
go.sum

@@ -4,6 +4,8 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 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/prologic/go-gopher v0.0.0-20181230133552-0c68ed5f58b0 h1:10LO/S8HVjIuEHsHea//Cena1Ztgy23f/e8HFC0w5ow=
+github.com/prologic/go-gopher v0.0.0-20181230133552-0c68ed5f58b0/go.mod h1:LiuwIXz4es4YIUOD6yRv8mES9n9dFbe4z0+TcrLkhXg=
 github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

+ 25 - 6
gopher.go

@@ -209,6 +209,25 @@ func ParseItem(line string) (item *Item, err error) {
 	return
 }
 
+// MarshalJSON serializes an Item into a JSON structure
+func (i Item) MarshalJSON() ([]byte, error) {
+	return json.Marshal(struct {
+		Type        string   `json:"type"`
+		Description string   `json:"description"`
+		Selector    string   `json:"selector"`
+		Host        string   `json:"host"`
+		Port        int      `json:"port"`
+		Extras      []string `json:"extras"`
+	}{
+		Type:        string(i.Type),
+		Description: i.Description,
+		Selector:    i.Selector,
+		Host:        i.Host,
+		Port:        i.Port,
+		Extras:      i.Extras,
+	})
+}
+
 // MarshalText serializes an Item into an array of bytes
 func (i Item) MarshalText() ([]byte, error) {
 	b := []byte{}
@@ -1004,7 +1023,7 @@ type ResponseWriter interface {
 	WriteInfo(msg string) error
 
 	// WriteItem writes an item
-	WriteItem(i *Item) error
+	WriteItem(i Item) error
 }
 
 // A response represents the server side of a Gopher response.
@@ -1044,7 +1063,7 @@ func (w *response) WriteError(err string) error {
 		return e
 	}
 
-	i := &Item{
+	i := Item{
 		Type:        ERROR,
 		Description: err,
 		Host:        "error.host",
@@ -1064,7 +1083,7 @@ func (w *response) WriteInfo(msg string) error {
 		return e
 	}
 
-	i := &Item{
+	i := Item{
 		Type:        INFO,
 		Description: msg,
 		Host:        "error.host",
@@ -1074,7 +1093,7 @@ func (w *response) WriteInfo(msg string) error {
 	return w.WriteItem(i)
 }
 
-func (w *response) WriteItem(i *Item) error {
+func (w *response) WriteItem(i Item) error {
 	if w.rt == 0 {
 		w.rt = 2
 	}
@@ -1247,7 +1266,7 @@ func dirList(w ResponseWriter, r *Request, f File, fs FileSystem) {
 				Error(w, "Error reading directory")
 				return
 			}
-			w.WriteItem(&Item{
+			w.WriteItem(Item{
 				Type:        DIRECTORY,
 				Description: file.Name(),
 				Selector:    pathname,
@@ -1266,7 +1285,7 @@ func dirList(w ResponseWriter, r *Request, f File, fs FileSystem) {
 
 			itemtype := GetItemType(path.Join(fullpath, file.Name()))
 
-			w.WriteItem(&Item{
+			w.WriteItem(Item{
 				Type:        itemtype,
 				Description: file.Name(),
 				Selector:    pathname,

+ 1 - 1
gopher_test.go

@@ -6,8 +6,8 @@ import (
 	"os"
 	"testing"
 
-	"github.com/prologic/go-gopher"
 	"github.com/stretchr/testify/assert"
+	"within.website/gopher"
 )
 
 func hello(w gopher.ResponseWriter, r *gopher.Request) {