github.com/tsuna/docker@v1.7.0-rc3/integration-cli/docker_cli_config_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  
    11  	"github.com/docker/docker/pkg/homedir"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerSuite) TestConfigHttpHeader(c *check.C) {
    16  	testRequires(c, UnixCli) // Can't set/unset HOME on windows right now
    17  	// We either need a level of Go that supports Unsetenv (for cases
    18  	// when HOME/USERPROFILE isn't set), or we need to be able to use
    19  	// os/user but user.Current() only works if we aren't statically compiling
    20  
    21  	var headers map[string][]string
    22  
    23  	server := httptest.NewServer(http.HandlerFunc(
    24  		func(w http.ResponseWriter, r *http.Request) {
    25  			headers = r.Header
    26  		}))
    27  	defer server.Close()
    28  
    29  	homeKey := homedir.Key()
    30  	homeVal := homedir.Get()
    31  	tmpDir, _ := ioutil.TempDir("", "fake-home")
    32  	defer os.RemoveAll(tmpDir)
    33  
    34  	dotDocker := filepath.Join(tmpDir, ".docker")
    35  	os.Mkdir(dotDocker, 0600)
    36  	tmpCfg := filepath.Join(dotDocker, "config.json")
    37  
    38  	defer func() { os.Setenv(homeKey, homeVal) }()
    39  	os.Setenv(homeKey, tmpDir)
    40  
    41  	data := `{
    42  		"HttpHeaders": { "MyHeader": "MyValue" }
    43  	}`
    44  
    45  	err := ioutil.WriteFile(tmpCfg, []byte(data), 0600)
    46  	if err != nil {
    47  		c.Fatalf("Err creating file(%s): %v", tmpCfg, err)
    48  	}
    49  
    50  	cmd := exec.Command(dockerBinary, "-H="+server.URL[7:], "ps")
    51  	out, _, _ := runCommandWithOutput(cmd)
    52  
    53  	if headers["Myheader"] == nil || headers["Myheader"][0] != "MyValue" {
    54  		c.Fatalf("Missing/bad header: %q\nout:%v", headers, out)
    55  	}
    56  }