test(cookies,encoding): refactor test to better readability and modifing
This commit is contained in:
@@ -2,60 +2,50 @@ package cookies
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestType struct {
|
||||
FirstValue string `cookie:"first_value"`
|
||||
SecondValue string `cookie:"second_value"`
|
||||
IntValue int `cookie:"int_value"`
|
||||
}
|
||||
|
||||
var TEST_VALUE = TestType{
|
||||
FirstValue: "Hello world",
|
||||
SecondValue: "This is a test",
|
||||
IntValue: -10,
|
||||
}
|
||||
|
||||
var TEST_TYPED_COOKIE = TypedCookie[TestType]{
|
||||
TypedValue: TEST_VALUE,
|
||||
}
|
||||
|
||||
var TEST_STRING = strings.Join([]string{
|
||||
"first_value:Hello world",
|
||||
"second_value:This is a test",
|
||||
"int_value:-10",
|
||||
}, "|")
|
||||
func TestMarshal(t *testing.T) {
|
||||
type testType struct {
|
||||
FirstValue string `cookie:"first_value"`
|
||||
SecondValue string `cookie:"second_value"`
|
||||
IntValue int `cookie:"int_value"`
|
||||
}
|
||||
|
||||
testValue := testType{
|
||||
FirstValue: "Hello world",
|
||||
SecondValue: "This is a test",
|
||||
IntValue: 10,
|
||||
}
|
||||
|
||||
c := TypedCookie[testType]{
|
||||
TypedValue: testValue,
|
||||
}
|
||||
|
||||
s, err := Marshal(c)
|
||||
s, err := Marshal(TEST_TYPED_COOKIE)
|
||||
if err != nil {
|
||||
t.Fatalf("Error trying to parse value:\n%s", err.Error())
|
||||
}
|
||||
|
||||
expected := "first_value:Hello world|second_value:This is a test|int_value:10"
|
||||
|
||||
if s.Value != expected {
|
||||
t.Fatalf("Assertion failed, expected:\n%s\n\nfound:\n%s", expected, s.Value)
|
||||
if s.Value != TEST_STRING {
|
||||
t.Fatalf("Assertion failed, expected:\n%s\n\nfound:\n%s", TEST_STRING, s.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
type TestType struct {
|
||||
FirstValue string `cookie:"first_value"`
|
||||
SecondValue string `cookie:"second_value"`
|
||||
}
|
||||
|
||||
c := http.Cookie{
|
||||
Value: "first_value:Hello world|second_value:This is a test",
|
||||
}
|
||||
|
||||
var tc TypedCookie[TestType]
|
||||
err := Unmarshal(c, &tc)
|
||||
err := Unmarshal(http.Cookie{Value: TEST_STRING}, &tc)
|
||||
if err != nil {
|
||||
t.Fatalf("Error trying to parse value:\n%s", err.Error())
|
||||
}
|
||||
|
||||
expected := TestType{
|
||||
FirstValue: "Hello world",
|
||||
SecondValue: "This is a test",
|
||||
}
|
||||
|
||||
if tc.TypedValue != expected {
|
||||
t.Fatalf("Assertion failed, expected:\n%s\n\nfound:\n%s", expected, tc.TypedValue)
|
||||
if tc.TypedValue != TEST_VALUE {
|
||||
t.Fatalf("Assertion failed, expected:\n%v\n\nfound:\n%v", TEST_VALUE, tc.TypedValue)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user