github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/tdata/tdata_test.go (about)

     1  package tdata
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"net/http"
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  type ResponseWriterMock struct {
    13  	Want    string
    14  	Test    *testing.T
    15  	Headers http.Header
    16  }
    17  
    18  func (r *ResponseWriterMock) Header() http.Header {
    19  	return r.Headers
    20  }
    21  
    22  func (r *ResponseWriterMock) Write(c []byte) (int, error) {
    23  	var got = string(c)
    24  
    25  	if r.Want != got {
    26  		r.Test.Errorf("Wanted %v, got %v instead", r.Want, got)
    27  	}
    28  
    29  	return len(c), nil
    30  }
    31  
    32  func (*ResponseWriterMock) WriteHeader(status int) {}
    33  
    34  func TestFromFile(t *testing.T) {
    35  	var want = FromFile("mocks/mock")
    36  	var got = "this is a mock\n"
    37  
    38  	if want != got {
    39  		t.Errorf("Wanted %v, got %v instead", got, want)
    40  	}
    41  }
    42  
    43  func TestFromFileNotFound(t *testing.T) {
    44  	var filename = fmt.Sprintf("not-found-%d", rand.Int())
    45  
    46  	defer func() {
    47  		r := recover()
    48  
    49  		if !os.IsNotExist(r.(error)) {
    50  			t.Errorf("Expected file %v to not exist", filename)
    51  		}
    52  	}()
    53  
    54  	FromFile(filename)
    55  }
    56  
    57  func TestToFile(t *testing.T) {
    58  	var tmp, err = ioutil.TempFile(os.TempDir(), "lcp")
    59  
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  
    64  	// IMPORTANT: If testing a string with "\n" Windows tests are going to fail.
    65  	ToFile(tmp.Name(), "foo")
    66  
    67  	var want = "foo"
    68  	var got = FromFile(tmp.Name())
    69  
    70  	if want != got {
    71  		t.Errorf("Wanted file contents doesn't match what was written.")
    72  	}
    73  
    74  	if err = tmp.Close(); err != nil {
    75  		panic(err)
    76  	}
    77  
    78  	if err = os.Remove(tmp.Name()); err != nil {
    79  		panic(err)
    80  	}
    81  }
    82  
    83  func TestToFilePanic(t *testing.T) {
    84  	defer func() {
    85  		r := recover()
    86  
    87  		if !os.IsNotExist(r.(error)) {
    88  			t.Errorf("Expected not to be able to open empty string filename file, got %v instead", r)
    89  		}
    90  	}()
    91  
    92  	// IMPORTANT: If testing a string with "\n" Windows tests are going to fail.
    93  	ToFile("", "foo")
    94  }
    95  
    96  func TestServerHandler(t *testing.T) {
    97  	var handler = ServerHandler("this is a mock\n")
    98  	var mock = &ResponseWriterMock{}
    99  	mock.Want = "this is a mock\n"
   100  	mock.Test = t
   101  	handler(mock, nil)
   102  }
   103  
   104  func TestServerJSONHandler(t *testing.T) {
   105  	var handler = ServerJSONHandler(`"this is a mock"`)
   106  	var mock = &ResponseWriterMock{
   107  		Headers: http.Header{},
   108  	}
   109  	mock.Want = `"this is a mock"`
   110  	mock.Test = t
   111  	handler(mock, nil)
   112  
   113  	var want = "application/json; charset=UTF-8"
   114  	var got = mock.Headers.Get("Content-Type")
   115  
   116  	if got != want {
   117  		t.Errorf("Wanted Content-Type %v, got %v instead", want, got)
   118  	}
   119  }
   120  
   121  func TestServerFileHandler(t *testing.T) {
   122  	var handler = ServerFileHandler("mocks/mock")
   123  	var mock = &ResponseWriterMock{}
   124  	mock.Want = "this is a mock\n"
   125  	mock.Test = t
   126  	handler(mock, nil)
   127  }
   128  
   129  func TestServerJSONFileHandler(t *testing.T) {
   130  	var handler = ServerJSONFileHandler("mocks/mock.json")
   131  	var mock = &ResponseWriterMock{
   132  		Headers: http.Header{},
   133  	}
   134  	mock.Want = `"this is a mock"`
   135  	mock.Test = t
   136  	handler(mock, nil)
   137  
   138  	var want = "application/json; charset=UTF-8"
   139  	var got = mock.Headers.Get("Content-Type")
   140  
   141  	if got != want {
   142  		t.Errorf("Wanted Content-Type %v, got %v instead", want, got)
   143  	}
   144  }