package server_test

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"git.netflux.io/rob/clipper/server"
	"github.com/stretchr/testify/assert"
)

func TestDisableDirectoryListings(t *testing.T) {
	testCases := []struct {
		path       string
		wantStatus int
	}{
		{
			path:       "/",
			wantStatus: http.StatusNotFound,
		},
		{
			path:       "/index.html",
			wantStatus: http.StatusOK,
		},
		{
			path:       "/foo",
			wantStatus: http.StatusOK,
		},
		{
			path:       "/foo/",
			wantStatus: http.StatusNotFound,
		},
		{
			path:       "/foo/bar",
			wantStatus: http.StatusOK,
		},
		{
			path:       "/foo/bar/",
			wantStatus: http.StatusNotFound,
		},
		{
			path:       "/foo/bar/baz",
			wantStatus: http.StatusOK,
		},
		{
			path:       "/foo/bar/baz/index.html",
			wantStatus: http.StatusOK,
		},
	}

	for _, tc := range testCases {
		t.Run("path="+tc.path, func(t *testing.T) {
			req := httptest.NewRequest("GET", tc.path, nil)
			rec := httptest.NewRecorder()

			handler := server.DisableDirectoryListings(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
				fmt.Fprintf(w, "Hello world")
			}))
			handler.ServeHTTP(rec, req)

			resp := rec.Result()
			assert.Equal(t, tc.wantStatus, resp.StatusCode)
		})
	}
}
