...

Source file src/github.com/go-resty/resty/v2/util_test.go

Documentation: github.com/go-resty/resty/v2

     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  
    13  func TestIsJSONType(t *testing.T) {
    14  	for _, test := range []struct {
    15  		input  string
    16  		expect bool
    17  	}{
    18  		{"application/json", true},
    19  		{"application/xml+json", true},
    20  		{"application/vnd.foo+json", true},
    21  
    22  		{"application/json; charset=utf-8", true},
    23  		{"application/vnd.foo+json; charset=utf-8", true},
    24  
    25  		{"text/json", true},
    26  		{"text/xml+json", true},
    27  		{"text/vnd.foo+json", true},
    28  
    29  		{"application/foo-json", false},
    30  		{"application/foo.json", false},
    31  		{"application/vnd.foo-json", false},
    32  		{"application/vnd.foo.json", false},
    33  		{"application/json+xml", false},
    34  
    35  		{"text/foo-json", false},
    36  		{"text/foo.json", false},
    37  		{"text/vnd.foo-json", false},
    38  		{"text/vnd.foo.json", false},
    39  		{"text/json+xml", false},
    40  	} {
    41  		result := IsJSONType(test.input)
    42  
    43  		if result != test.expect {
    44  			t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
    45  		}
    46  	}
    47  }
    48  
    49  func TestIsXMLType(t *testing.T) {
    50  	for _, test := range []struct {
    51  		input  string
    52  		expect bool
    53  	}{
    54  		{"application/xml", true},
    55  		{"application/json+xml", true},
    56  		{"application/vnd.foo+xml", true},
    57  
    58  		{"application/xml; charset=utf-8", true},
    59  		{"application/vnd.foo+xml; charset=utf-8", true},
    60  
    61  		{"text/xml", true},
    62  		{"text/json+xml", true},
    63  		{"text/vnd.foo+xml", true},
    64  
    65  		{"application/foo-xml", false},
    66  		{"application/foo.xml", false},
    67  		{"application/vnd.foo-xml", false},
    68  		{"application/vnd.foo.xml", false},
    69  		{"application/xml+json", false},
    70  
    71  		{"text/foo-xml", false},
    72  		{"text/foo.xml", false},
    73  		{"text/vnd.foo-xml", false},
    74  		{"text/vnd.foo.xml", false},
    75  		{"text/xml+json", false},
    76  	} {
    77  		result := IsXMLType(test.input)
    78  
    79  		if result != test.expect {
    80  			t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
    81  		}
    82  	}
    83  }
    84  
    85  func TestWriteMultipartFormFileReaderEmpty(t *testing.T) {
    86  	w := multipart.NewWriter(bytes.NewBuffer(nil))
    87  	defer func() { _ = w.Close() }()
    88  	if err := writeMultipartFormFile(w, "foo", "bar", bytes.NewReader(nil)); err != nil {
    89  		t.Errorf("Got unexpected error: %v", err)
    90  	}
    91  }
    92  
    93  func TestWriteMultipartFormFileReaderError(t *testing.T) {
    94  	err := writeMultipartFormFile(nil, "", "", &brokenReadCloser{})
    95  	assertNotNil(t, err)
    96  	assertEqual(t, "read error", err.Error())
    97  }
    98  

View as plain text