github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/httpserver/request_handler_test.go (about)

     1  // Copyright (c) 2015-2021, NVIDIA CORPORATION.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package httpserver
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"net/http/httptest"
    10  	"testing"
    11  )
    12  
    13  func TestConfigExpansion(t *testing.T) {
    14  	testSetup(t)
    15  	defer testTeardown(t)
    16  
    17  	testConfigExpansion := func(url string, shouldBeExpanded bool) {
    18  		req := httptest.NewRequest("GET", "http://pfs.com"+url, nil)
    19  		w := httptest.NewRecorder()
    20  
    21  		doGet(w, req)
    22  		resp := w.Result()
    23  		body, _ := ioutil.ReadAll(resp.Body)
    24  
    25  		if resp.StatusCode != 200 {
    26  			t.Errorf("[%s]: Config response was %d; expected 200", url, resp.StatusCode)
    27  			return
    28  		}
    29  
    30  		crs := bytes.Count(body, []byte("\n"))
    31  		if shouldBeExpanded {
    32  			if crs <= 2 {
    33  				t.Errorf("[%s]: Only found %d <CR>s, but config should be expanded.", url, crs)
    34  			}
    35  		} else {
    36  			if crs > 2 {
    37  				t.Errorf("[%s]: Found %d <CR>s, but config should be compact.", url, crs)
    38  			}
    39  		}
    40  	}
    41  
    42  	// NOTE:  These are really routing tests.  If and when we isolate the routing code, we can
    43  	// move tests like this there.  If and when we use an off-the-shelf router, we can delete
    44  	// these altogether and just call the internal function for a few canonical urls.
    45  	expandedUrls := []string{
    46  		"/config",
    47  		"/config/?",
    48  		"/config/?foo=bar",
    49  		"/config/?compact=false",
    50  	}
    51  
    52  	compactUrls := []string{
    53  		"/config?compact=true",
    54  		"/config?compact=1",
    55  		"/config/?foo=bar&compact=1&baz=quux",
    56  	}
    57  
    58  	for _, url := range expandedUrls {
    59  		testConfigExpansion(url, true)
    60  	}
    61  
    62  	for _, url := range compactUrls {
    63  		testConfigExpansion(url, false)
    64  	}
    65  
    66  	return
    67  }