aboutsummaryrefslogtreecommitdiff
path: root/internal/modules/middleware_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/modules/middleware_test.go')
-rw-r--r--internal/modules/middleware_test.go216
1 files changed, 216 insertions, 0 deletions
diff --git a/internal/modules/middleware_test.go b/internal/modules/middleware_test.go
new file mode 100644
index 0000000..e1868a7
--- /dev/null
+++ b/internal/modules/middleware_test.go
@@ -0,0 +1,216 @@
+package modules
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+func TestIsModuleRequest(t *testing.T) {
+ tests := []struct {
+ name string
+ url string
+ want bool
+ method string
+ }{
+ {
+ name: "go-import request",
+ url: "/example.com/mymodule?go-get=1",
+ want: true,
+ },
+ {
+ name: "version list endpoint",
+ url: "/example.com/mymodule/@v/list",
+ want: true,
+ },
+ {
+ name: "latest version endpoint",
+ url: "/example.com/mymodule/@latest",
+ want: true,
+ },
+ {
+ name: "version info endpoint",
+ url: "/example.com/mymodule/@v/v1.0.0.info",
+ want: true,
+ },
+ {
+ name: "mod file endpoint",
+ url: "/example.com/mymodule/@v/v1.0.0.mod",
+ want: true,
+ },
+ {
+ name: "zip file endpoint",
+ url: "/example.com/mymodule/@v/v1.0.0.zip",
+ want: true,
+ },
+ {
+ name: "regular git request",
+ url: "/example.com/mymodule.git/info/refs",
+ want: false,
+ },
+ {
+ name: "regular path",
+ url: "/example.com/mymodule/README.md",
+ want: false,
+ },
+ {
+ name: "go-get with different value",
+ url: "/example.com/mymodule?go-get=0",
+ want: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ req := httptest.NewRequest(http.MethodGet, tt.url, nil)
+ got := isModuleRequest(req)
+ if got != tt.want {
+ t.Errorf("isModuleRequest() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestIsModuleProxyPath(t *testing.T) {
+ tests := []struct {
+ name string
+ path string
+ want bool
+ }{
+ {
+ name: "version list",
+ path: "/example.com/mymodule/@v/list",
+ want: true,
+ },
+ {
+ name: "latest version",
+ path: "/example.com/mymodule/@latest",
+ want: true,
+ },
+ {
+ name: "version info",
+ path: "/example.com/mymodule/@v/v1.0.0.info",
+ want: true,
+ },
+ {
+ name: "mod file",
+ path: "/example.com/mymodule/@v/v1.0.0.mod",
+ want: true,
+ },
+ {
+ name: "zip file",
+ path: "/example.com/mymodule/@v/v1.0.0.zip",
+ want: true,
+ },
+ {
+ name: "invalid @v path",
+ path: "/example.com/mymodule/@v/invalid",
+ want: false,
+ },
+ {
+ name: "regular path",
+ path: "/example.com/mymodule/file.go",
+ want: false,
+ },
+ {
+ name: "git path",
+ path: "/example.com/mymodule.git/info/refs",
+ want: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := isModuleProxyPath(tt.path)
+ if got != tt.want {
+ t.Errorf("isModuleProxyPath() = %v, want %v for path %s", got, tt.want, tt.path)
+ }
+ })
+ }
+}
+
+func TestExtractModulePath(t *testing.T) {
+ tests := []struct {
+ name string
+ path string
+ want string
+ }{
+ {
+ name: "version list path",
+ path: "/example.com/mymodule/@v/list",
+ want: "example.com/mymodule",
+ },
+ {
+ name: "latest version path",
+ path: "/example.com/mymodule/@latest",
+ want: "example.com/mymodule",
+ },
+ {
+ name: "version info path",
+ path: "/example.com/mymodule/@v/v1.0.0.info",
+ want: "example.com/mymodule",
+ },
+ {
+ name: "simple module path",
+ path: "/example.com/mymodule",
+ want: "example.com/mymodule",
+ },
+ {
+ name: "nested module path",
+ path: "/github.com/user/repo/submodule/@v/list",
+ want: "github.com/user/repo/submodule",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := ExtractModulePath(tt.path)
+ if got != tt.want {
+ t.Errorf("ExtractModulePath() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestExtractVersion(t *testing.T) {
+ tests := []struct {
+ name string
+ path string
+ want string
+ }{
+ {
+ name: "version info",
+ path: "/example.com/mymodule/@v/v1.0.0.info",
+ want: "v1.0.0",
+ },
+ {
+ name: "mod file",
+ path: "/example.com/mymodule/@v/v1.2.3.mod",
+ want: "v1.2.3",
+ },
+ {
+ name: "zip file",
+ path: "/example.com/mymodule/@v/v2.0.0-beta.1.zip",
+ want: "v2.0.0-beta.1",
+ },
+ {
+ name: "no version in path",
+ path: "/example.com/mymodule/@v/list",
+ want: "",
+ },
+ {
+ name: "latest endpoint",
+ path: "/example.com/mymodule/@latest",
+ want: "",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := ExtractVersion(tt.path)
+ if got != tt.want {
+ t.Errorf("ExtractVersion() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}