本文整理汇总了Golang中github.com/rafaeljusto/shelter/testing/utils.BuildHTTPHeader函数的典型用法代码示例。如果您正苦于以下问题:Golang BuildHTTPHeader函数的具体用法?Golang BuildHTTPHeader怎么用?Golang BuildHTTPHeader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BuildHTTPHeader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: domainCacheTest
func domainCacheTest(database *mgo.Database, r *http.Request,
requestContent, header string, domainCacheTestData []DomainCacheTestData) {
mux := handy.NewHandy()
h := new(handler.DomainHandler)
mux.Handle("/domain/{fqdn}", func() handy.Handler {
return h
})
for _, item := range domainCacheTestData {
r.Header.Set(header, item.HeaderValue)
if len(requestContent) > 0 {
utils.BuildHTTPHeader(r, []byte(requestContent))
} else {
utils.BuildHTTPHeader(r, nil)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != item.ExpectedHTTPStatus {
utils.Fatalln(fmt.Sprintf("Error in %s test using %s [%s] "+
"HTTP header. Expected HTTP status code %d and got %d",
r.Method, header, item.HeaderValue, item.ExpectedHTTPStatus,
w.Code), errors.New(string(responseContent)))
}
}
}
示例2: retrieveScansMetadata
func retrieveScansMetadata(database *mgo.Database) {
mux := handy.NewHandy()
h := new(handler.ScansHandler)
mux.Handle("/scans", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", "/scans/?orderby=startedat:desc&pagesize=10&page=1", nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
response1 := *h.Response
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving scans", errors.New(string(responseContent)))
}
r, err = http.NewRequest("HEAD", "/scans/?orderby=startedat:desc&pagesize=10&page=1", nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
mux.ServeHTTP(w, r)
response2 := *h.Response
responseContent, err = ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving scans", errors.New(string(responseContent)))
}
if !utils.CompareProtocolScans(response1, response2) {
utils.Fatalln("At this point the GET and HEAD method should "+
"return the same body content", nil)
}
}
示例3: retrieveScanMetadata
func retrieveScanMetadata(database *mgo.Database, startedAt time.Time) {
mux := handy.NewHandy()
h := new(handler.ScanHandler)
mux.Handle("/scan/{started-at}", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
response1 := *h.Response
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
}
r, err = http.NewRequest("HEAD", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
mux.ServeHTTP(w, r)
response2 := *h.Response
responseContent, err = ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
}
if !utils.CompareProtocolScan(response1, response2) {
utils.Fatalln("At this point the GET and HEAD method should "+
"return the same body content", nil)
}
}
示例4: retrieveDomainMetadata
func retrieveDomainMetadata(database *mgo.Database) {
mux := handy.NewHandy()
h := new(handler.DomainHandler)
mux.Handle("/domain/{fqdn}", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
response1 := *h.Response
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body from GET", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
}
r, err = http.NewRequest("HEAD", "/domain/example.com.br.", nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
mux.ServeHTTP(w, r)
response2 := *h.Response
responseContent, err = ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body from HEAD", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
}
if !utils.CompareProtocolDomain(response1, response2) {
utils.Fatalln("At this point the GET and HEAD method should "+
"return the same body content", nil)
}
}
示例5: retrieveScansIfNoneMatch
func retrieveScansIfNoneMatch(database *mgo.Database) {
mux := handy.NewHandy()
h := new(handler.ScansHandler)
mux.Handle("/scans", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", "/scans", nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
scansCacheTest(database, r, "If-None-Match", []ScansCacheTestData{
{
HeaderValue: w.Header().Get("ETag"),
ExpectedHTTPStatus: http.StatusNotModified,
},
{
HeaderValue: w.Header().Get("ETag") + "x",
ExpectedHTTPStatus: http.StatusOK,
},
})
}
示例6: retrieveUnknownScan
func retrieveUnknownScan(database *mgo.Database, startedAt time.Time) {
mux := handy.NewHandy()
h := new(handler.ScanHandler)
mux.Handle("/scan/{started-at}", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Add(-1*time.Millisecond).Format(time.RFC3339Nano)), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusNotFound {
utils.Fatalln("Error retrieving unknown scan", errors.New(string(responseContent)))
}
}
示例7: deleteDomains
func deleteDomains(database *mgo.Database) {
mux := handy.NewHandy()
h := new(handler.DomainHandler)
mux.Handle("/domain/{fqdn}", func() handy.Handler {
return h
})
for i := 0; i < 100; i++ {
r, err := http.NewRequest("DELETE", fmt.Sprintf("/domain/example%d.com.br.", i), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusNoContent {
utils.Fatalln("Error removing domain", errors.New(string(responseContent)))
}
}
}
示例8: retrieveUnknownDomain
func retrieveUnknownDomain(database *mgo.Database) {
mux := handy.NewHandy()
h := new(handler.DomainHandler)
mux.Handle("/domain/{fqdn}", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusNotFound {
utils.Fatalln("Error retrieving unknown domain", errors.New(string(responseContent)))
}
}
示例9: retrieveDomainsWithCache
func retrieveDomainsWithCache() {
var client http.Client
url := ""
if len(config.ShelterConfig.WebClient.Listeners) > 0 {
url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
config.ShelterConfig.WebClient.Listeners[0].Port)
}
if len(url) == 0 {
utils.Fatalln("There's no interface to connect to", nil)
}
var r *http.Request
var err error
r, err = http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/domains"), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
response, err := client.Do(r)
if err != nil {
utils.Fatalln("Error sending request", err)
}
r, err = http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/domains"), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
r.Header.Add("If-None-Match", response.Header.Get("ETag"))
utils.BuildHTTPHeader(r, nil)
response, err = client.Do(r)
if err != nil {
utils.Fatalln("Error sending request", err)
}
if response.StatusCode != http.StatusNotModified {
utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET and URI /domains",
http.StatusNotModified, response.StatusCode), nil)
}
}
示例10: createDomains
func createDomains() {
var client http.Client
url := ""
if len(config.ShelterConfig.WebClient.Listeners) > 0 {
url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
config.ShelterConfig.WebClient.Listeners[0].Port)
}
if len(url) == 0 {
utils.Fatalln("There's no interface to connect to", nil)
}
var r *http.Request
var err error
for i := 0; i < 100; i++ {
uri := fmt.Sprintf("/domain/example%d.com.br.", i)
content := `{
"Nameservers": [
{ "host": "a.dns.br." },
{ "host": "b.dns.br." }
],
"Owners": [
{ "email": "[email protected]", "language": "pt-br" }
]
}`
r, err = http.NewRequest("PUT", fmt.Sprintf("%s%s", url, uri),
bytes.NewReader([]byte(content)))
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, []byte(content))
response, err := client.Do(r)
if err != nil {
utils.Fatalln("Error sending request", err)
}
if response.StatusCode != http.StatusCreated {
responseContent, err := ioutil.ReadAll(response.Body)
if err == nil {
utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method PUT and URI %s",
http.StatusCreated, response.StatusCode, uri),
errors.New(string(responseContent)),
)
} else {
utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method PUT and URI %s",
http.StatusCreated, response.StatusCode, uri), nil)
}
}
}
}
示例11: retrieveScan
func retrieveScan(database *mgo.Database, startedAt time.Time) {
mux := handy.NewHandy()
h := new(handler.ScanHandler)
mux.Handle("/scan/{started-at}", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
}
if w.Header().Get("ETag") != "1" {
utils.Fatalln("Not setting ETag in scan retrieve response", nil)
}
if len(w.Header().Get("Last-Modified")) == 0 {
utils.Fatalln("Not setting Last-Modified in scan retrieve response", nil)
}
if h.Response.Status != "EXECUTED" {
utils.Fatalln("Scan's status was not persisted correctly", nil)
}
if h.Response.DomainsScanned != 5000000 {
utils.Fatalln("Scan's domains scanned information is wrong", nil)
}
if h.Response.DomainsWithDNSSECScanned != 250000 {
utils.Fatalln("Scan's domains with DNSSEC scanned information is wrong", nil)
}
if h.Response.NameserverStatistics["OK"] != 4800000 ||
h.Response.NameserverStatistics["TIMEOUT"] != 200000 {
utils.Fatalln("Scan's nameserver statistics are wrong", nil)
}
if h.Response.DSStatistics["OK"] != 220000 ||
h.Response.DSStatistics["EXPSIG"] != 30000 {
utils.Fatalln("Scan's nameserver statistics are wrong", nil)
}
}
示例12: retrieveCurrentScan
func retrieveCurrentScan() {
var client http.Client
url := ""
if len(config.ShelterConfig.WebClient.Listeners) > 0 {
url = fmt.Sprintf("http://%s:%d", config.ShelterConfig.WebClient.Listeners[0].IP,
config.ShelterConfig.WebClient.Listeners[0].Port)
}
if len(url) == 0 {
utils.Fatalln("There's no interface to connect to", nil)
}
r, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, "/scans/?current"), nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
response, err := client.Do(r)
if err != nil {
utils.Fatalln("Error sending request", err)
}
responseContent, err := ioutil.ReadAll(response.Body)
if err != nil {
utils.Fatalln("Error reading response content", err)
}
if response.StatusCode != http.StatusOK {
utils.Fatalln(fmt.Sprintf("Expected HTTP status %d and got %d for method GET "+
"and URI /scan/currentscan",
http.StatusOK, response.StatusCode),
errors.New(string(responseContent)),
)
}
var scansResponse protocol.ScansResponse
if err := json.Unmarshal(responseContent, &scansResponse); err != nil {
utils.Fatalln("Error decoding scan response", err)
}
if len(scansResponse.Scans) != 1 {
utils.Fatalln("Not returning the current scan in the result set or "+
"more items than expected were found", nil)
}
if scansResponse.Scans[0].Status != model.ScanStatusToString(model.ScanStatusWaitingExecution) {
utils.Fatalln(fmt.Sprintf("Invalid status returned by current scan. Expected %s and got %s",
model.ScanStatusToString(model.ScanStatusWaitingExecution), scansResponse.Scans[0].Status), nil)
}
}
示例13: retrieveDomain
func retrieveDomain(database *mgo.Database) {
mux := handy.NewHandy()
h := new(handler.DomainHandler)
mux.Handle("/domain/{fqdn}", func() handy.Handler {
return h
})
r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusOK {
utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
}
if w.Header().Get("ETag") != "2" {
utils.Fatalln("Not setting ETag in domain retrieve response", nil)
}
if len(w.Header().Get("Last-Modified")) == 0 {
utils.Fatalln("Not setting Last-Modified in domain retrieve response", nil)
}
if h.Response.FQDN != "example.com.br." {
utils.Fatalln("Domain's FQDN was not persisted correctly", nil)
}
if len(h.Response.Nameservers) != 2 ||
h.Response.Nameservers[0].Host != "ns1.example.com.br." ||
h.Response.Nameservers[0].IPv4 != "127.0.0.1" ||
h.Response.Nameservers[1].Host != "ns3.example.com.br." ||
h.Response.Nameservers[1].IPv6 != "::1" {
utils.Fatalln("Domain's nameservers were not persisted correctly", nil)
}
if len(h.Response.Owners) != 1 ||
h.Response.Owners[0].Email != "[email protected]" {
utils.Fatalln("Domain's owners were not persisted correctly", nil)
}
}
示例14: updateDomain
func updateDomain(database *mgo.Database) {
mux := handy.NewHandy()
h := new(handler.DomainHandler)
mux.Handle("/domain/{fqdn}", func() handy.Handler {
return h
})
requestContent := `{
"Nameservers": [
{ "host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
{ "host": "ns3.example.com.br.", "ipv6": "::1" }
],
"Owners": [
{ "email": "[email protected]", "language": "pt-br" }
]
}`
r, err := http.NewRequest("PUT", "/domain/example.com.br.", strings.NewReader(requestContent))
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, []byte(requestContent))
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
responseContent, err := ioutil.ReadAll(w.Body)
if err != nil {
utils.Fatalln("Error reading response body", err)
}
if w.Code != http.StatusNoContent {
utils.Fatalln(fmt.Sprintf("Error updating domain. "+
"Expecting %d and got %d", http.StatusNoContent, w.Code),
errors.New(string(responseContent)))
}
if w.Header().Get("ETag") != "2" {
utils.Fatalln("Not setting ETag in domain update response", nil)
}
if len(w.Header().Get("Last-Modified")) == 0 {
utils.Fatalln("Not setting Last-Modified in domain update response", nil)
}
if len(w.Header().Get("Location")) > 0 {
utils.Fatalln("Setting Location in domain update response", nil)
}
}
示例15: restActionReport
func restActionReport(numberOfItems int,
content []byte,
requestBuilder func(int) (*http.Request, error),
expectedStatus int,
action string) string {
var client http.Client
totalDuration, domainsPerSecond := calculateRESTDurations(func() {
for i := 0; i < numberOfItems; i++ {
r, err := requestBuilder(i)
if err != nil {
utils.Fatalln("Error creating the HTTP request", err)
}
utils.BuildHTTPHeader(r, content)
response, err := client.Do(r)
if err != nil {
utils.Fatalln(fmt.Sprintf("Error detected when sending request for action %s"+
" and URI \"%s\"", action, r.URL.RequestURI()), err)
}
_, err = ioutil.ReadAll(response.Body)
if err != nil {
utils.Fatalln(fmt.Sprintf("Error detected when reading the response body for action %s"+
" and URI \"%s\"", action, r.URL.RequestURI()), err)
}
response.Body.Close()
if response.StatusCode != expectedStatus {
utils.Fatalln(fmt.Sprintf("Error with the domain object in the action %s. "+
"Expected HTTP status %d and got %d",
action, expectedStatus, response.StatusCode), nil)
}
}
}, numberOfItems)
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
return fmt.Sprintf("% -8d | %-9s | %16s | %4d | %14.2f\n",
numberOfItems,
action,
time.Duration(int64(totalDuration)).String(),
domainsPerSecond,
float64(memStats.Alloc)/float64(MB),
)
}