github.com/louisevanderlith/droxolite@v1.20.2/sample/apiclient_test.go (about)

     1  package sample
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"github.com/gorilla/mux"
     7  	"log"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"github.com/louisevanderlith/droxolite/sample/clients"
    12  )
    13  
    14  func TestStore_Get_OK(t *testing.T) {
    15  	rr, err := GetResponse(apiRoutes(), "/fake/store", nil)
    16  
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	if rr.Code != http.StatusOK {
    22  		t.Fatal(rr.Body.String())
    23  	}
    24  
    25  	t.Log(rr.Body.String())
    26  
    27  	var result []string
    28  	err = json.Unmarshal(rr.Body.Bytes(), &result)
    29  
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	expected := []string{"Berry", "Orange", "Apple"}
    35  
    36  	for i := 0; i < len(result); i++ {
    37  		if result[i] != expected[i] {
    38  			t.Fatalf("unexpected body: got %v want %v", result[i], expected[i])
    39  		}
    40  	}
    41  }
    42  
    43  func TestStore_GetOne_OK(t *testing.T) {
    44  	rr, err := GetResponse(apiRoutes(), "/fake/store/1560674025%601", nil)
    45  
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	if rr.Code != http.StatusOK {
    51  		t.Fatal(rr.Body.String())
    52  		return
    53  	}
    54  
    55  	t.Log(rr.Body.String())
    56  
    57  	result := ""
    58  	err = json.Unmarshal(rr.Body.Bytes(), &result)
    59  
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	expected := "Got a Key 1560674025`1"
    65  	if result != expected {
    66  		t.Errorf("unexpected body: got %v want %v",
    67  			result, expected)
    68  	}
    69  }
    70  
    71  func TestMain_API_NameAndIdParam_OK(t *testing.T) {
    72  	rr, err := GetResponse(apiRoutes(), "/fake/Jimmy/73", nil)
    73  
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if rr.Code != http.StatusOK {
    79  		t.Fatal(rr.Body.String())
    80  		return
    81  	}
    82  
    83  	result := ""
    84  	err = json.Unmarshal(rr.Body.Bytes(), &result)
    85  
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	expected := "Jimmy is 73"
    91  	if result != expected {
    92  		t.Errorf("unexpected body: got %v want %v", result, expected)
    93  	}
    94  }
    95  
    96  func TestMain_API_HuskKey_Escaped_OK(t *testing.T) {
    97  	rr, err := GetResponse(apiRoutes(), "/fake/1560674025%601", nil)
    98  
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	result := ""
   104  	err = json.Unmarshal(rr.Body.Bytes(), &result)
   105  
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	expected := "Got a Key 1560674025`1"
   111  	if result != expected {
   112  		t.Errorf("unexpected body: got %v want %v",
   113  			result, expected)
   114  	}
   115  }
   116  
   117  func TestMain_API_HuskKey_OK(t *testing.T) {
   118  	rr, err := GetResponse(apiRoutes(), "/fake/store/1563985947336`12", nil)
   119  
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	log.Println(rr.Body.String())
   124  	result := ""
   125  	err = json.Unmarshal(rr.Body.Bytes(), &result)
   126  
   127  	if err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	expected := "Got a Key 1563985947336`12"
   132  	if result != expected {
   133  		t.Errorf("unexpected body: got %v want %v",
   134  			result, expected)
   135  	}
   136  }
   137  
   138  func TestMain_API_PageSize_OK(t *testing.T) {
   139  	rr, err := GetResponse(apiRoutes(), "/fake/store/C78/eyJuYW1lIjogIkppbW15IiwiYWdlOiB7ICJtb250aCI6IDIsICJkYXRlIjogOCwgInllYXIiOiAxOTkxfSwiYWxpdmUiOiB0cnVlfQ==", nil)
   140  
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  
   145  	result := ""
   146  	err = json.Unmarshal(rr.Body.Bytes(), &result)
   147  
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  
   152  	expected := "Page 3, Size 78"
   153  	if result != expected {
   154  		t.Errorf("unexpected body: got %v want %v",
   155  			result, expected)
   156  	}
   157  }
   158  
   159  func TestMain_API_BooleanParam_OK(t *testing.T) {
   160  	rr, err := GetResponse(apiRoutes(), "/fake/question/false", nil)
   161  
   162  	if err != nil {
   163  		t.Fatal(err)
   164  	}
   165  
   166  	result := ""
   167  	err = json.Unmarshal(rr.Body.Bytes(), &result)
   168  
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	expected := "Thanks for Nothing!"
   174  	if result != expected {
   175  		t.Errorf("unexpected body: got %v want %v",
   176  			result, expected)
   177  	}
   178  }
   179  
   180  func TestMain_API_HashParam_OK(t *testing.T) {
   181  	rr, err := GetResponse(apiRoutes(), "/fake/store/A1/eyJuYW1lIjogIkppbW15IiwiYWdlOiB7ICJtb250aCI6IDIsICJkYXRlIjogOCwgInllYXIiOiAxOTkxfSwiYWxpdmUiOiB0cnVlfQ==", nil)
   182  
   183  	if err != nil {
   184  		t.Fatal(err)
   185  	}
   186  
   187  	if rr.Code != http.StatusOK {
   188  		t.Fatal(rr.Body.String())
   189  	}
   190  
   191  	result := ""
   192  	err = json.Unmarshal(rr.Body.Bytes(), &result)
   193  
   194  	if err != nil {
   195  		t.Fatal(err)
   196  	}
   197  
   198  	expected := `{"name": "Jimmy","age: { "month": 2, "date": 8, "year": 1991},"alive": true}`
   199  	if result != expected {
   200  		t.Errorf("unexpected body: got %v want %v",
   201  			result, expected)
   202  	}
   203  }
   204  
   205  func TestMain_API_POST_OK(t *testing.T) {
   206  	body, err := json.Marshal(struct{ Act string }{"Jump"})
   207  
   208  	if err != nil {
   209  		t.Fatal(err)
   210  	}
   211  
   212  	readr := bytes.NewBuffer(body)
   213  	rr, err := GetResponse(apiRoutes(), "/fake/store/73", readr)
   214  
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  
   219  	if rr.Code != http.StatusOK {
   220  		t.Fatal(rr.Body.String())
   221  	}
   222  
   223  	result := ""
   224  	err = json.Unmarshal(rr.Body.Bytes(), &result)
   225  
   226  	if err != nil {
   227  		t.Fatal(err)
   228  	}
   229  
   230  	expected := "#73: Jump"
   231  	if result != expected {
   232  		t.Errorf("unexpected body: got %v want %v",
   233  			result, expected)
   234  	}
   235  }
   236  
   237  func apiRoutes() http.Handler {
   238  	r := mux.NewRouter()
   239  	fke := r.PathPrefix("/fake").Subrouter()
   240  
   241  	fke.HandleFunc("/store", clients.StoreGet).Methods(http.MethodGet)
   242  	fke.HandleFunc("/store/{key:[0-9]+\x60[0-9]+}", clients.StoreView).Methods(http.MethodGet)
   243  	fke.HandleFunc("/store/{pagesize:[A-Z][0-9]+}/{hash:[a-zA-Z0-9]+={0,2}}", clients.StoreSearch).Methods(http.MethodGet)
   244  	fke.HandleFunc("/store/{pagesize:[A-Z][0-9]+}", clients.StoreSearch).Methods(http.MethodGet)
   245  	fke.HandleFunc("/store", clients.StoreCreate).Methods(http.MethodPost)
   246  	fke.HandleFunc("/store/{key:[0-9]+`[0-9]+}", clients.StoreUpdate).Methods(http.MethodPut)
   247  	fke.HandleFunc("/store/{key:[0-9]+`[0-9]+}", clients.StoreDelete).Methods(http.MethodDelete)
   248  
   249  	return r
   250  }