本文整理汇总了Golang中net/http/httptest.Server.URL方法的典型用法代码示例。如果您正苦于以下问题:Golang Server.URL方法的具体用法?Golang Server.URL怎么用?Golang Server.URL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http/httptest.Server
的用法示例。
在下文中一共展示了Server.URL方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestHttpBinary
func TestHttpBinary(t *testing.T) {
tests := []struct {
description string
fromFile bool // true = --from-file, false = --from-dir/--from-archive
urlPath string
statusCode int // server status code, 200 if not set
contentDisposition bool // will server send Content-Disposition with filename?
networkError bool
tlsBadCert bool
expectedError string
expectWarning bool
}{
{
description: "--from-file, filename in header",
fromFile: true,
urlPath: "/",
contentDisposition: true,
},
{
description: "--from-file, filename in URL",
fromFile: true,
urlPath: "/hi.txt",
},
{
description: "--from-file, no filename",
fromFile: true,
urlPath: "",
expectedError: "unable to determine filename",
},
{
description: "--from-file, http error",
fromFile: true,
urlPath: "/",
statusCode: 404,
expectedError: "unable to download file",
},
{
description: "--from-file, network error",
fromFile: true,
urlPath: "/hi.txt",
networkError: true,
expectedError: "invalid port",
},
{
description: "--from-file, https with invalid certificate",
fromFile: true,
urlPath: "/hi.txt",
tlsBadCert: true,
expectedError: "certificate signed by unknown authority",
},
{
description: "--from-dir, filename in header",
fromFile: false,
contentDisposition: true,
expectWarning: true,
},
{
description: "--from-dir, filename in URL",
fromFile: false,
urlPath: "/hi.tar.gz",
expectWarning: true,
},
{
description: "--from-dir, no filename",
fromFile: false,
expectWarning: true,
},
{
description: "--from-dir, http error",
statusCode: 503,
fromFile: false,
expectedError: "unable to download file",
},
}
for _, tc := range tests {
stdin := bytes.NewReader([]byte{})
stdout := &bytes.Buffer{}
options := buildapi.BinaryBuildRequestOptions{}
fakeclient := testclient.NewSimpleFake()
buildconfigs := FakeBuildConfigs{fakeclient.BuildConfigs("default"), t, tc.fromFile}
handler := func(w http.ResponseWriter, r *http.Request) {
if tc.contentDisposition {
w.Header().Add("Content-Disposition", "attachment; filename=hi.txt")
}
if tc.statusCode > 0 {
w.WriteHeader(tc.statusCode)
}
w.Write([]byte("hi"))
}
var server *httptest.Server
if tc.tlsBadCert {
// uses self-signed certificate
server = httptest.NewTLSServer(http.HandlerFunc(handler))
} else {
server = httptest.NewServer(http.HandlerFunc(handler))
}
defer server.Close()
//.........这里部分代码省略.........