Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions certgen_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package devd

import (
"io/ioutil"
"os"
"path"
"testing"
)

func TestGenerateCert(t *testing.T) {
d, err := ioutil.TempDir("", "devdtest")
d, err := os.MkdirTemp("", "devdtest")
if err != nil {
t.Error(err)
return
Expand Down
12 changes: 4 additions & 8 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package fileserver

import (
"context"
"errors"
"fmt"
"html/template"
Expand All @@ -18,8 +19,6 @@ import (
"strings"
"time"

"golang.org/x/net/context"

"github.com/cortesi/devd/inject"
"github.com/cortesi/devd/routespec"
"github.com/cortesi/termlog"
Expand Down Expand Up @@ -115,7 +114,7 @@ func serveContent(ci inject.CopyInject, w http.ResponseWriter, r *http.Request,
var buf [sniffLen]byte
n, _ := io.ReadFull(content, buf[:])
ctype = http.DetectContentType(buf[:n])
_, err := content.Seek(0, os.SEEK_SET) // rewind to output whole file
_, err := content.Seek(0, io.SeekStart) // rewind to output whole file
if err != nil {
http.Error(w, "seeker can't seek", http.StatusInternalServerError)
return err
Expand Down Expand Up @@ -229,7 +228,7 @@ func localRedirect(w http.ResponseWriter, r *http.Request, newPath string) {
// To use the operating system's file system implementation,
// use http.Dir:
//
// http.Handle("/", &fileserver.FileServer{Root: http.Dir("/tmp")})
// http.Handle("/", &fileserver.FileServer{Root: http.Dir("/tmp")})
type FileServer struct {
Version string
Root http.FileSystem
Expand Down Expand Up @@ -301,10 +300,7 @@ func _getType(ext string) string {
func matchTypes(spec string, req string) bool {
smime := _getType(path.Ext(spec))
rmime := _getType(path.Ext(req))
if smime == rmime {
return true
}
return false
return smime == rmime
}

func (fserver *FileServer) serve404(w http.ResponseWriter) error {
Expand Down
72 changes: 11 additions & 61 deletions fileserver/fileserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ package fileserver

import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -17,7 +15,6 @@ import (
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -49,11 +46,11 @@ func ServeFile(w http.ResponseWriter, r *http.Request, name string) {

func ServeContent(w http.ResponseWriter, req *http.Request, name string, modtime time.Time, content io.ReadSeeker) error {
sizeFunc := func() (int64, error) {
size, err := content.Seek(0, os.SEEK_END)
size, err := content.Seek(0, io.SeekEnd)
if err != nil {
return 0, errSeeker
}
_, err = content.Seek(0, os.SEEK_SET)
_, err = content.Seek(0, io.SeekStart)
if err != nil {
return 0, errSeeker
}
Expand All @@ -67,12 +64,6 @@ const (
testFileLen = 11
)

type wantRange struct {
start, end int64 // range [start,end)
}

var itoa = strconv.Itoa

var notFoundSearchPathsSpecs = []struct {
path string
spec string
Expand Down Expand Up @@ -131,7 +122,7 @@ func TestServeFile(t *testing.T) {

var err error

file, err := ioutil.ReadFile(testFile)
file, err := os.ReadFile(testFile)
if err != nil {
t.Fatal("reading file:", err)
}
Expand Down Expand Up @@ -188,47 +179,6 @@ func TestFSRedirect(t *testing.T) {
}
}

type testFileSystem struct {
open func(name string) (http.File, error)
}

func (fs *testFileSystem) Open(name string) (http.File, error) {
return fs.open(name)
}

func _TestFileServerCleans(t *testing.T) {
defer afterTest(t)
ch := make(chan string, 1)
fs := &FileServer{
"version",
&testFileSystem{
func(name string) (http.File, error) {
ch <- name
return nil, errors.New("file does not exist")
},
},
inject.CopyInject{},
ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
[]routespec.RouteSpec{},
"",
}
tests := []struct {
reqPath, openArg string
}{
{"/foo.txt", "/foo.txt"},
{"/../foo.txt", "/foo.txt"},
}
req, _ := http.NewRequest("GET", "http://example.com", nil)
for n, test := range tests {
rec := httptest.NewRecorder()
req.URL.Path = test.reqPath
fs.ServeHTTP(rec, req)
if got := <-ch; got != test.openArg {
t.Errorf("test %d: got %q, want %q", n, got, test.openArg)
}
}
}

func mustRemoveAll(dir string) {
err := os.RemoveAll(dir)
if err != nil {
Expand All @@ -238,12 +188,12 @@ func mustRemoveAll(dir string) {

func TestFileServerImplicitLeadingSlash(t *testing.T) {
defer afterTest(t)
tempDir, err := ioutil.TempDir("", "")
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("TempDir: %v", err)
}
defer mustRemoveAll(tempDir)
if err := ioutil.WriteFile(filepath.Join(tempDir, "foo.txt"), []byte("Hello world"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(tempDir, "foo.txt"), []byte("Hello world"), 0644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
fs := &FileServer{
Expand All @@ -262,7 +212,7 @@ func TestFileServerImplicitLeadingSlash(t *testing.T) {
if err != nil {
t.Fatalf("Get %s: %v", suffix, err)
}
b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("ReadAll %s: %v", suffix, err)
}
Expand Down Expand Up @@ -427,7 +377,7 @@ func TestServeIndexHtml(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal("reading Body:", err)
}
Expand Down Expand Up @@ -455,7 +405,7 @@ func TestFileServerZeroByte(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal("reading Body:", err)
}
Expand Down Expand Up @@ -623,7 +573,7 @@ func TestDirectoryIfNotModified(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(res.Body)
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -806,7 +756,7 @@ func TestServeContent(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = io.Copy(ioutil.Discard, res.Body)
_, err = io.Copy(io.Discard, res.Body)
if err != nil {
t.Fatal(err)
}
Expand All @@ -828,7 +778,7 @@ func getBody(t *testing.T, testName string, req http.Request) (*http.Response, [
if err != nil {
t.Fatalf("%s: for URL %q, send error: %v", testName, req.URL.String(), err)
}
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("%s: for URL %q, reading body: %v", testName, req.URL.String(), err)
}
Expand Down
3 changes: 1 addition & 2 deletions httpctx/httpctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
package httpctx

import (
"context"
"net/http"
"strings"

"golang.org/x/net/context"
)

// Handler is a request handler with an added context
Expand Down
4 changes: 2 additions & 2 deletions inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (ci *CopyInject) Sniff(src io.Reader, contentType string) (Injector, error)
return nil, fmt.Errorf("inject could not read data to sniff: %s", err)
}
injector.sniffedData = buf[:n]
if bytes.Index(buf, ci.Payload) > -1 {
if bytes.Contains(buf, ci.Payload) {
return injector, nil
}
loc := ci.Marker.FindIndex(injector.sniffedData[:min(n, ci.Within)])
Expand All @@ -114,7 +114,7 @@ func (ci *CopyInject) Sniff(src io.Reader, contentType string) (Injector, error)

// ServeTemplate renders and serves a template to an http.ResponseWriter
func (ci *CopyInject) ServeTemplate(statuscode int, w http.ResponseWriter, t *template.Template, data interface{}) error {
buff := bytes.NewBuffer(make([]byte, 0, 0))
buff := bytes.NewBuffer(make([]byte, 0))
err := t.Execute(buff, data)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions inject/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func inject(ci CopyInject, data string, contentType string) (found bool, dstdata
if err != nil {
return false, "", err
}
return injector.Found(), string(dst.Bytes()), nil
return injector.Found(), dst.String(), nil
}

func TestReverseProxyNoInject(t *testing.T) {
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestReverseProxy(t *testing.T) {
if err != nil {
t.Errorf("Test %d, unexpected error:\n%s\n", i, err)
}
if found && strings.Index(dst, tt.payload) == -1 {
if found && !strings.Contains(dst, tt.payload) {
t.Errorf(
"Test %d, payload not found.", i,
)
Expand Down Expand Up @@ -110,8 +110,8 @@ func TestNil(t *testing.T) {
t.Error("Unexpected")
}
dst := bytes.NewBuffer(make([]byte, 0))
injector.Copy(dst)
if string(dst.Bytes()) != val {
t.Errorf("Expected %s, got %s", val, string(dst.Bytes()))
_, _ = injector.Copy(dst)
if dst.String() != val {
t.Errorf("Expected %s, got %s", val, dst.String())
}
}
2 changes: 1 addition & 1 deletion responselogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (rl *ResponseLogWriter) logCode(code int, status string) {
if err != nil {
rl.Log.Warn("Invalid content-length header")
} else if cli > 0 {
clstr = fmt.Sprintf("%s", humanize.Bytes(uint64(cli)))
clstr = humanize.Bytes(uint64(cli))
}
}
rl.Log.Say("<- %s %s", codestr, clstr)
Expand Down
3 changes: 1 addition & 2 deletions reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package reverseproxy

import (
"context"
"fmt"
"io"
"net"
Expand All @@ -14,8 +15,6 @@ import (
"sync"
"time"

"golang.org/x/net/context"

"github.com/cortesi/devd/inject"
"github.com/cortesi/termlog"
humanize "github.com/dustin/go-humanize"
Expand Down
Loading