komuw / ong

ong, is a Go http toolkit.
MIT License
16 stars 4 forks source link

add mux tests that re-uses https://github.com/benhoyt/go-routing #340

Open komuw opened 11 months ago

komuw commented 11 months ago
komuw commented 5 months ago
package mux

import (
    "bytes"
    "context"
    "fmt"
    "net/http"
    "net/http/httptest"
    "strings"
    "testing"

    "github.com/komuw/ong/config"
    "github.com/komuw/ong/internal/tst"
    "github.com/komuw/ong/log"
    "go.akshayshah.org/attest"
)

// TODO: get the license of `benhoyt/go-routing`
// From: https://github.com/benhoyt/go-routing

func TestGoRouting(t *testing.T) {
    t.Parallel()

    t.Run("success", func(t *testing.T) {
        t.Parallel()

        l := log.New(context.Background(), &bytes.Buffer{}, 500)

        // r.HandleFunc("GET /{$}", home)
        // r.HandleFunc("GET /contact", contact)
        // r.HandleFunc("GET /api/widgets", apiGetWidgets)
        // r.HandleFunc("POST /api/widgets", apiCreateWidget)

        m := New(
            config.WithOpts("localhost", 443, tst.SecretKey(), config.DirectIpStrategy, l),
            nil,
            NewRoute(
                "/",
                MethodGet,
                http.HandlerFunc(home),
            ),
            NewRoute(
                "/contact",
                MethodGet,
                http.HandlerFunc(contact),
            ),
            NewRoute(
                "/api/widgets",
                MethodGet,
                http.HandlerFunc(apiGetWidgets),
            ),
            NewRoute(
                "/api/widgets",
                MethodPost,
                http.HandlerFunc(apiCreateWidget),
            ),
        )

        tests := []struct {
            method string
            path   string
            status int
            body   string
        }{
            {"GET", "/", 200, "home\n"},
            {"POST", "/", 405, ""},

            {"GET", "/contact", 200, "contact\n"},
            {"POST", "/contact", 405, ""},
            {"GET", "/contact/", 404, ""},
            {"GET", "/contact/no", 404, ""},

            {"GET", "/api/widgets", 200, "apiGetWidgets\n"},
            {"GET", "/api/widgets/", 404, ""},

            {"POST", "/api/widgets", 200, "apiCreateWidget\n"},
            {"POST", "/api/widgets/", 404, ""},
        }

        for _, test := range tests {
            path := strings.ReplaceAll(test.path, "/", "_")

            t.Run(test.method+path, func(t *testing.T) {
                recorder := httptest.NewRecorder()
                request, err := http.NewRequest(test.method, test.path, &bytes.Buffer{})
                attest.Ok(t, err)

                m.ServeHTTP(recorder, request)

                if recorder.Code != test.status {
                    t.Fatalf("expected status %d, got %d", test.status, recorder.Code)
                }
                if test.status == 200 {
                    body := recorder.Body.String()
                    if body != test.body {
                        t.Fatalf("expected body %q, got %q", test.body, body)
                    }
                }
            })
        }
    })
}

func home(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "home\n")
}

func contact(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "contact\n")
}

func apiGetWidgets(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "apiGetWidgets\n")
}

func apiCreateWidget(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "apiCreateWidget\n")
}

// func apiUpdateWidget(w http.ResponseWriter, r *http.Request) {
//  slug := mux.PathValue(r, "slug")
//  fmt.Fprintf(w, "apiUpdateWidget %s\n", slug)
// }

// func apiCreateWidgetPart(w http.ResponseWriter, r *http.Request) {
//  slug := mux.PathValue(r, "slug")
//  fmt.Fprintf(w, "apiCreateWidgetPart %s\n", slug)
// }

// func apiUpdateWidgetPart(w http.ResponseWriter, r *http.Request) {
//  slug := mux.PathValue(r, "slug")
//  idStr := mux.PathValue(r, "id")
//  id, err := strconv.Atoi(idStr)
//  if err != nil || id <= 0 {
//      http.NotFound(w, r)
//      return
//  }
//  fmt.Fprintf(w, "apiUpdateWidgetPart %s %d\n", slug, id)
// }

// func apiDeleteWidgetPart(w http.ResponseWriter, r *http.Request) {
//  slug := mux.PathValue(r, "slug")
//  idStr := mux.PathValue(r, "id")
//  id, err := strconv.Atoi(idStr)
//  if err != nil || id <= 0 {
//      http.NotFound(w, r)
//      return
//  }
//  fmt.Fprintf(w, "apiDeleteWidgetPart %s %d\n", slug, id)
// }

// func widgetGet(w http.ResponseWriter, r *http.Request) {
//  slug := mux.PathValue(r, "slug")
//  fmt.Fprintf(w, "widget %s\n", slug)
// }

// func widgetAdmin(w http.ResponseWriter, r *http.Request) {
//  slug := mux.PathValue(r, "slug")
//  fmt.Fprintf(w, "widgetAdmin %s\n", slug)
// }

// func widgetImage(w http.ResponseWriter, r *http.Request) {
//  slug := mux.PathValue(r, "slug")
//  fmt.Fprintf(w, "widgetImage %s\n", slug)
// }
komuw commented 5 months ago

https://github.com/benhoyt/go-routing/issues/5