Socialify

Folder ..

Viewing helpers_test.go
111 lines (91 loc) • 2.5 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
package tests

import (
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"net/url"
	"reflect"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type testCase struct {
	description  string
	method       string
	url          string
	formData     url.Values
	headers      map[string]string
	expectedCode int
	validate     func(*testing.T, *http.Response, []byte)
}

func testRequest(t *testing.T, tc testCase) (*http.Response, []byte) {
	req, err := http.NewRequest(tc.method, tc.url, strings.NewReader(tc.formData.Encode()))
	require.NoError(t, err, "Failed to create request")

	for key, value := range tc.headers {
		req.Header.Set(key, value)
	}

	resp, err := TestApp.Test(req, -1)
	require.NoError(t, err, "Request failed")

	body, err := io.ReadAll(resp.Body)
	require.NoError(t, err, "Failed to read response body")

	slog.Info("Test response",
		"method", tc.method,
		"url", tc.url,
		"status", resp.StatusCode,
		"body", string(body))

	return resp, body
}

func runTests(t *testing.T, tests []testCase) {
	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			resp, body := testRequest(t, test)

			assert.Equal(t, test.expectedCode, resp.StatusCode, "Unexpected status code")

			if test.validate != nil {
				test.validate(t, resp, body)
			}
		})
	}
}

func buildFormData(data interface{}) url.Values {
	formData := url.Values{}
	v := reflect.ValueOf(data)

	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}

	if v.Kind() != reflect.Struct {
		return formData
	}

	t := v.Type()
	for i := 0; i < v.NumField(); i++ {
		field := t.Field(i)
		value := v.Field(i)

		// Check if the field should be ignored
		if field.Tag.Get("form") == "-" {
			continue
		}

		// Get the form field name from the tag or use the struct field name
		fieldName := field.Tag.Get("form")
		if fieldName == "" {
			fieldName = field.Name
		}

		// Handle different types of fields
		switch value.Kind() {
		case reflect.String:
			if value.String() != "" {
				formData.Set(fieldName, value.String())
			}
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			formData.Set(fieldName, fmt.Sprintf("%d", value.Int()))
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
			formData.Set(fieldName, fmt.Sprintf("%d", value.Uint()))
		case reflect.Float32, reflect.Float64:
			formData.Set(fieldName, fmt.Sprintf("%f", value.Float()))
		case reflect.Bool:
			formData.Set(fieldName, fmt.Sprintf("%t", value.Bool()))
			// TODO: Add more types as needed
		}
	}

	return formData
}