github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/httpserver/request_handler_test.go (about)

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