github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/resty/util_test.go (about)

     1  // Copyright (c) 2015-2021 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
     2  // resty source code and usage is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  package resty
     6  
     7  import (
     8  	"bytes"
     9  	"mime/multipart"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestIsJSONType(t *testing.T) {
    16  	for _, test := range []struct {
    17  		input  string
    18  		expect bool
    19  	}{
    20  		{"application/json", true},
    21  		{"application/xml+json", true},
    22  		{"application/vnd.foo+json", true},
    23  
    24  		{"application/json; charset=utf-8", true},
    25  		{"application/vnd.foo+json; charset=utf-8", true},
    26  
    27  		{"text/json", true},
    28  		{"text/xml+json", true},
    29  		{"text/vnd.foo+json", true},
    30  
    31  		{"application/foo-json", false},
    32  		{"application/foo.json", false},
    33  		{"application/vnd.foo-json", false},
    34  		{"application/vnd.foo.json", false},
    35  		{"application/json+xml", false},
    36  
    37  		{"text/foo-json", false},
    38  		{"text/foo.json", false},
    39  		{"text/vnd.foo-json", false},
    40  		{"text/vnd.foo.json", false},
    41  		{"text/json+xml", false},
    42  	} {
    43  		result := IsJSONType(test.input)
    44  
    45  		if result != test.expect {
    46  			t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
    47  		}
    48  	}
    49  }
    50  
    51  func TestIsXMLType(t *testing.T) {
    52  	for _, test := range []struct {
    53  		input  string
    54  		expect bool
    55  	}{
    56  		{"application/xml", true},
    57  		{"application/json+xml", true},
    58  		{"application/vnd.foo+xml", true},
    59  
    60  		{"application/xml; charset=utf-8", true},
    61  		{"application/vnd.foo+xml; charset=utf-8", true},
    62  
    63  		{"text/xml", true},
    64  		{"text/json+xml", true},
    65  		{"text/vnd.foo+xml", true},
    66  
    67  		{"application/foo-xml", false},
    68  		{"application/foo.xml", false},
    69  		{"application/vnd.foo-xml", false},
    70  		{"application/vnd.foo.xml", false},
    71  		{"application/xml+json", false},
    72  
    73  		{"text/foo-xml", false},
    74  		{"text/foo.xml", false},
    75  		{"text/vnd.foo-xml", false},
    76  		{"text/vnd.foo.xml", false},
    77  		{"text/xml+json", false},
    78  	} {
    79  		result := IsXMLType(test.input)
    80  
    81  		if result != test.expect {
    82  			t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
    83  		}
    84  	}
    85  }
    86  
    87  func TestWriteMultipartFormFileReaderEmpty(t *testing.T) {
    88  	w := multipart.NewWriter(bytes.NewBuffer(nil))
    89  	defer func() { _ = w.Close() }()
    90  	if err := writeMultipartFormFile(w, "foo", "bar", bytes.NewReader(nil)); err != nil {
    91  		t.Errorf("Got unexpected error: %v", err)
    92  	}
    93  }
    94  
    95  func TestWriteMultipartFormFileReaderError(t *testing.T) {
    96  	err := writeMultipartFormFile(nil, "", "", &brokenReadCloser{})
    97  	assert.NotNil(t, err)
    98  	assert.Equal(t, "read error", err.Error())
    99  }