mirror of
https://github.com/taigrr/pastebin
synced 2026-04-09 07:01:21 -07:00
- Update module path from prologic/pastebin to taigrr/pastebin - Replace go.rice with stdlib embed for templates and static assets - Replace httprouter with stdlib net/http ServeMux (Go 1.22+) - Replace namsral/flag with cobra + fang for CLI argument parsing - Fix inverted TLS insecure logic in client (was negating the flag) - Close HTTP response body in client - Use base64 URLEncoding instead of StdEncoding for paste IDs - Add comprehensive handler tests for all endpoints - Add client package tests - Add utils tests for RandomString - Remove templates.go (replaced by embed) - Remove unused dependencies (go-metrics, stats, logger, httprouter, go.rice)
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package client
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPasteSuccess(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
err := r.ParseForm()
|
|
require.NoError(t, err)
|
|
blob := r.FormValue("blob")
|
|
assert.Equal(t, "test content", blob)
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
|
|
cli := NewClient(server.URL, false)
|
|
err := cli.Paste(strings.NewReader("test content"))
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestPasteServerError(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer server.Close()
|
|
|
|
cli := NewClient(server.URL, false)
|
|
err := cli.Paste(strings.NewReader("test content"))
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "unexpected response")
|
|
}
|
|
|
|
func TestPasteInvalidURL(t *testing.T) {
|
|
cli := NewClient("http://invalid.invalid.invalid:99999", false)
|
|
err := cli.Paste(strings.NewReader("test content"))
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
cli := NewClient("http://example.com", true)
|
|
assert.Equal(t, "http://example.com", cli.url)
|
|
assert.True(t, cli.insecure)
|
|
}
|