當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Server.Config方法代碼示例

本文整理匯總了Golang中net/http/httptest.Server.Config方法的典型用法代碼示例。如果您正苦於以下問題:Golang Server.Config方法的具體用法?Golang Server.Config怎麽用?Golang Server.Config使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http/httptest.Server的用法示例。


在下文中一共展示了Server.Config方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: testUpgradeWithCurl

func testUpgradeWithCurl(t *testing.T, mode testUpgradeMode, lenient bool) {
	if runtime.GOOS != "linux" {
		t.Skip("skipping Docker test when not on Linux; requires --net which won't work with boot2docker anyway")
	}
	requireCurl(t)
	const msg = "Hello from curl!\n"
	var ts2 *httptest.Server
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Foo", "Bar")
		w.Header().Set("Client-Proto", r.Proto)
		io.WriteString(w, msg)
	})

	ts := httptest.NewUnstartedServer(handler)
	switch mode {
	case testUpgradeNoTLS:
		ts.TLS = nil
	default:
		ts2 = httptest.NewUnstartedServer(handler)
		ts2.TLS = nil
	}
	wrapped := UpgradeServer(ts.Config, &Server{
		PermitProhibitedCipherSuites: lenient,
	})
	modes := make([]testUpgradeMode, 0, 2)
	switch mode {
	case testUpgradeNoTLS:
		modes = append(modes, mode)
		ts.Config = wrapped
		ts.Start()
		t.Logf("Running test server for curl to hit at: %s", ts.URL)
	case testUpgradeDualTLS:
		modes = append(modes, testUpgradeNoTLS)
		fallthrough
	default:
		modes = append(modes, testUpgradeTLSOnly)
		ts2.Config = wrapped
		ts.TLS = ts.Config.TLSConfig // the httptest.Server has its own copy of this TLS config
		ts.StartTLS()
		ts2.Start()
		defer ts2.Close()
		t.Logf("Running test server for curl to hit at: %s and %s", ts.URL, ts2.URL)
	}
	defer ts.Close()
	defer func() { testHookOnConn = nil }()
	for _, mode := range modes {
		var gotConn int32

		testHookOnConn = func() { atomic.StoreInt32(&gotConn, 1) }

		T := ts
		if mode == testUpgradeNoTLS && ts2 != nil {
			T = ts2
		}
		container := curl(t, "-D-", "--silent", "--http2", "--insecure", T.URL)
		defer kill(container)
		resc := make(chan interface{}, 1)
		go func() {
			res, err := dockerLogs(container)
			if err != nil {
				resc <- err
			} else {
				resc <- res
			}
		}()
		select {
		case res := <-resc:
			if err, ok := res.(error); ok {
				t.Fatal(err)
			}
			testDualUpgradeOutput(t, string(res.([]byte)), (T.TLS == nil),
				"HTTP/2.0 200",
				"foo:Bar",
				"client-proto:HTTP/2",
				msg)
		case <-time.After(3 * time.Second):
			t.Errorf("timeout waiting for curl")
		}

		if atomic.LoadInt32(&gotConn) == 0 {
			t.Error("never saw an http2 connection")
		}
	}
}
開發者ID:youleiy,項目名稱:http2,代碼行數:84,代碼來源:upgrade_test.go


注:本文中的net/http/httptest.Server.Config方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。