github.com/gofiber/fiber/v2@v2.47.0/utils/json_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  type sampleStructure struct {
     9  	ImportantString string `json:"important_string"`
    10  }
    11  
    12  func Test_GolangJSONEncoder(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	var (
    16  		ss = &sampleStructure{
    17  			ImportantString: "Hello World",
    18  		}
    19  		importantString             = `{"important_string":"Hello World"}`
    20  		jsonEncoder     JSONMarshal = json.Marshal
    21  	)
    22  
    23  	raw, err := jsonEncoder(ss)
    24  	AssertEqual(t, err, nil)
    25  
    26  	AssertEqual(t, string(raw), importantString)
    27  }
    28  
    29  func Test_DefaultJSONEncoder(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	var (
    33  		ss = &sampleStructure{
    34  			ImportantString: "Hello World",
    35  		}
    36  		importantString             = `{"important_string":"Hello World"}`
    37  		jsonEncoder     JSONMarshal = json.Marshal
    38  	)
    39  
    40  	raw, err := jsonEncoder(ss)
    41  	AssertEqual(t, err, nil)
    42  
    43  	AssertEqual(t, string(raw), importantString)
    44  }
    45  
    46  func Test_DefaultJSONDecoder(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	var (
    50  		ss              sampleStructure
    51  		importantString               = []byte(`{"important_string":"Hello World"}`)
    52  		jsonDecoder     JSONUnmarshal = json.Unmarshal
    53  	)
    54  
    55  	err := jsonDecoder(importantString, &ss)
    56  	AssertEqual(t, err, nil)
    57  	AssertEqual(t, "Hello World", ss.ImportantString)
    58  }