aboutsummaryrefslogtreecommitdiff
path: root/handler_test.go
diff options
context:
space:
mode:
authorGravatar Abhinav Gupta <mail@abhinavg.net> 2023-01-23 10:28:04 -0800
committerGravatar GitHub <noreply@github.com> 2023-01-23 10:28:04 -0800
commit5f327a458f6488d053815621cd1a5154f1ce76c0 (patch)
tree6856030c5ebec8581ce417c5c4e62e07a4346387 /handler_test.go
parent86218534c831b3fc6fdfb98f5ad334262d7060ae (diff)
downloadsally-5f327a458f6488d053815621cd1a5154f1ce76c0.tar.gz
sally-5f327a458f6488d053815621cd1a5154f1ce76c0.tar.zst
sally-5f327a458f6488d053815621cd1a5154f1ce76c0.zip
Drop httprouter dependency (#71)
This drops the third-party HTTP router dependency. This dependency wasn't strictly necessary since our routing needs are quite basic: - `/$name` and `/$name/*` for all registered packages - `/` for root This is easily accomplished with `http.ServeMux`: - register `/$name` and `/$name/`. The latter will receive all subpackage requests. - register `/` and reject anything that isn't for exactly `/`.
Diffstat (limited to 'handler_test.go')
-rw-r--r--handler_test.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/handler_test.go b/handler_test.go
index e300d19..b1d5d2e 100644
--- a/handler_test.go
+++ b/handler_test.go
@@ -1,9 +1,14 @@
package main
import (
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
var config = `
@@ -120,3 +125,44 @@ func TestPackageLevelURL(t *testing.T) {
</html>
`)
}
+
+func TestPostRejected(t *testing.T) {
+ t.Parallel()
+
+ h := CreateHandler(&Config{
+ URL: "go.uberalt.org",
+ Packages: map[string]Package{
+ "zap": {
+ Repo: "github.com/uber-go/zap",
+ },
+ },
+ })
+ srv := httptest.NewServer(h)
+ t.Cleanup(srv.Close)
+
+ tests := []struct {
+ desc string
+ path string
+ }{
+ {desc: "index", path: "/"},
+ {desc: "package", path: "/zap"},
+ {desc: "subpackage", path: "/zap/zapcore"},
+ }
+
+ for _, tt := range tests {
+ tt := tt
+ t.Run(tt.desc, func(t *testing.T) {
+ t.Parallel()
+
+ res, err := http.Post(srv.URL+tt.path, "text/plain", strings.NewReader("foo"))
+ require.NoError(t, err)
+ defer res.Body.Close()
+
+ body, err := io.ReadAll(res.Body)
+ require.NoError(t, err)
+
+ assert.Equal(t, http.StatusNotFound, res.StatusCode,
+ "expected 404, got:\n%s", string(body))
+ })
+ }
+}