本文整理匯總了Golang中net/url.String函數的典型用法代碼示例。如果您正苦於以下問題:Golang String函數的具體用法?Golang String怎麽用?Golang String使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了String函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGetRejectsWrongEnvUUIDPath
func (s *charmsSuite) TestGetRejectsWrongEnvUUIDPath(c *gc.C) {
url := s.charmsURL(c, "url=local:quantal/dummy-1&file=revision")
url.Path = "/environment/dead-beef-123456/charms"
resp, err := s.authRequest(c, "GET", url.String(), "", nil)
c.Assert(err, jc.ErrorIsNil)
s.assertErrorResponse(c, resp, http.StatusNotFound, `unknown environment: "dead-beef-123456"`)
}
示例2: buildURL
func (p *Prerender) buildURL(or *http.Request) string {
url := p.Options.PrerenderURL
if !strings.HasSuffix(url.String(), "/") {
url.Path = url.Path + "/"
}
var protocol = or.URL.Scheme
if cf := or.Header.Get("CF-Visitor"); cf != "" {
match := cfSchemeRegex.FindStringSubmatch(cf)
if len(match) > 1 {
protocol = match[1]
}
}
if len(protocol) == 0 {
protocol = "http"
}
if fp := or.Header.Get("X-Forwarded-Proto"); fp != "" {
protocol = strings.Split(fp, ",")[0]
}
apiURL := url.String() + protocol + "://" + or.Host + or.URL.Path + "?" +
or.URL.RawQuery
return apiURL
}
示例3: Test_parseSearchResponse
func Test_parseSearchResponse(t *testing.T) {
responseBody := "HTTP/1.1 200 OK\r\n" +
"CACHE-CONTROL: max-age=100\r\n" +
"EXT:\r\n" +
"LOCATION: http://10.1.2.3:80/description.xml\r\n" +
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\r\n" +
"ST: upnp:rootdevice\r\n" +
"USN: uuid:2f402f80-da50-11e1-9b23-0017880a4c69::upnp:rootdevice\r\n" +
"Date: Sun, 18 Aug 2013 08:49:37 GMT\r\n" +
"\r\n"
responseAddr, _ := net.ResolveUDPAddr("udp", "10.1.2.3:1900")
response, err := parseSearchResponse(strings.NewReader(responseBody), responseAddr)
if err != nil {
t.Fatal("Error while parsing the response.", err)
}
assertEqual(t, "max-age=100", response.Control, "response.Control")
assertEqual(t, "FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1", response.Server, "response.Server")
assertEqual(t, "upnp:rootdevice", response.ST, "response.ST")
assertEqual(t, "", response.Ext, "response.Ext")
assertEqual(t, "uuid:2f402f80-da50-11e1-9b23-0017880a4c69::upnp:rootdevice", response.USN, "response.USN")
assertEqual(t, responseAddr, response.ResponseAddr, "response.Addr")
url, _ := url.Parse("http://10.1.2.3:80/description.xml")
if url.String() != response.Location.String() {
t.Errorf("%q is not equal to %q. %q", url.String(), response.Location.String(), "response.Location")
}
gmt, _ := time.LoadLocation("UTC")
date := time.Date(2013, time.August, 18, 8, 49, 37, 0, gmt)
assertEqual(t, date, response.Date, "response.Date")
}
示例4: GetHandler
func GetHandler(w http.ResponseWriter, req *http.Request, s Server) error {
vars := mux.Vars(req)
key := "/" + vars["key"]
// Help client to redirect the request to the current leader
if req.FormValue("consistent") == "true" && s.State() != raft.Leader {
leader := s.Leader()
hostname, _ := s.ClientURL(leader)
url, err := url.Parse(hostname)
if err != nil {
log.Warn("Redirect cannot parse hostName ", hostname)
return err
}
url.RawQuery = req.URL.RawQuery
url.Path = req.URL.Path
log.Debugf("Redirect consistent get to %s", url.String())
http.Redirect(w, req, url.String(), http.StatusTemporaryRedirect)
return nil
}
recursive := (req.FormValue("recursive") == "true")
sort := (req.FormValue("sorted") == "true")
waitIndex := req.FormValue("waitIndex")
stream := (req.FormValue("stream") == "true")
if req.FormValue("wait") == "true" {
return handleWatch(key, recursive, stream, waitIndex, w, s)
}
return handleGet(key, recursive, sort, w, s)
}
示例5: Search
func (c *Cluster) Search(search Search) (*http.Response, error) {
url := c.URL
url.Path = search.Path()
query := search.Query()
if search.Type() != SEARCH_TYPE_SCROLL {
query["search_type"] = []string{search.Type().String()}
}
url.RawQuery = query.Encode()
var (
req *http.Request
createErr error
)
body := search.Data()
if body == nil {
req, createErr = http.NewRequest("GET", url.String(), nil)
} else {
var buffer bytes.Buffer
if err := json.NewEncoder(&buffer).Encode(body); err != nil {
return nil, err
}
req, createErr = http.NewRequest("POST", url.String(), &buffer)
}
if nil != createErr {
return nil, createErr
}
return (&http.Client{}).Do(req)
}
示例6: updateURLs
// updateURLs checks and updates any of n's attributes that are listed in tagsToAttrs.
// Any URLs found are, if they're relative, updated with the necessary changes to make
// a visit to that URL also go through the proxy.
// sourceURL is the URL of the page which we're currently on; it's required to make
// relative links work.
func (t *proxyTransport) updateURLs(n *html.Node, sourceURL *url.URL) {
if n.Type != html.ElementNode {
return
}
attrs, ok := tagsToAttrs[n.Data]
if !ok {
return
}
for i, attr := range n.Attr {
if !attrs.Has(attr.Key) {
continue
}
url, err := url.Parse(attr.Val)
if err != nil {
continue
}
// Is this URL relative?
if url.Host == "" {
url.Scheme = t.proxyScheme
url.Host = t.proxyHost
url.Path = path.Join(t.proxyPathPrepend, path.Dir(sourceURL.Path), url.Path, "/")
n.Attr[i].Val = url.String()
} else if url.Host == sourceURL.Host {
url.Scheme = t.proxyScheme
url.Host = t.proxyHost
url.Path = path.Join(t.proxyPathPrepend, url.Path)
n.Attr[i].Val = url.String()
}
}
}
示例7: post
func (b *Bucket) post(location string, body []byte) ([]byte, error) {
cli := &http.Client{}
url, err := b.CabinetUrl.Parse(location)
if err != nil {
return nil, fmt.Errorf("cannot parse url %s", location)
}
req, err := http.NewRequest("POST", url.String(), bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("cannot create request %s", url.String())
}
req.SetBasicAuth(Option.AdminUser, Option.AdminPass)
resp, err := cli.Do(req)
if err != nil {
return nil, fmt.Errorf("error request to %s, %s", url.String(), err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("response code not 200 OK but %d, %s", resp.StatusCode, url.String())
}
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("cannot read body %s", url.String())
}
return resp_body, nil
}
示例8: ServerGroups
// ServerGroups returns the supported groups, with information like supported versions and the
// preferred version.
func (d *DiscoveryClient) ServerGroups() (apiGroupList *unversioned.APIGroupList, err error) {
// Get the groupVersions exposed at /api
url := d.baseURL
url.Path = "/api"
resp, err := d.get(url.String())
if err != nil {
return nil, err
}
var v unversioned.APIVersions
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, fmt.Errorf("unexpected error: %v", err)
}
apiGroup := apiVersionsToAPIGroup(&v)
// Get the groupVersions exposed at /apis
url.Path = "/apis"
resp2, err := d.get(url.String())
if err != nil {
return nil, err
}
defer resp2.Body.Close()
apiGroupList = &unversioned.APIGroupList{}
if err = json.NewDecoder(resp2.Body).Decode(&apiGroupList); err != nil {
return nil, fmt.Errorf("unexpected error: %v", err)
}
// append the group retrieved from /api to the list
apiGroupList.Groups = append(apiGroupList.Groups, apiGroup)
return apiGroupList, nil
}
示例9: createRequest
// createRequest creates instance of http.Request containing all config
// values and URL from current client and provided body.
func createRequest(cl *Client, data io.Reader) (r *http.Request, err error) {
url, err := createUrl(cl)
if err != nil {
return nil, err
}
var req *http.Request
if data != nil {
if req, err = http.NewRequest(cl.request.method, url.String(), data); err != nil {
return nil, err
}
} else {
if req, err = http.NewRequest(cl.request.method, url.String(), nil); err != nil {
return nil, err
}
}
for key, val := range cl.Headers {
req.Header.Set(key, val)
}
if req.Header.Get(HeaderUserAgent) == "" {
req.Header.Set(HeaderUserAgent, defaultUserAgentHeader)
}
if req.Header.Get(HeaderContentType) == "" {
req.Header.Set(HeaderContentType, defaultContentType)
}
if req.Header.Get(HeaderAccept) == "" {
req.Header.Set(HeaderAccept, defaultAcceptHeader)
}
return req, nil
}
示例10: Get
func (c Client) Get(uri string) (Collection, int, error) {
var collection Collection
var tmp_single NodeCollection
var tmp_multiple NodesCollection
var nodes []Node
var status int
url, err := url.Parse(uri)
fmt.Printf("Retrieving data from %+v\n", url.String())
resp, err := http.Get(url.String())
if err != nil {
fmt.Printf("%+v\n", err)
status = resp.StatusCode
return collection, status, err
} else {
status = resp.StatusCode
}
// Shock returns different structure if data not found
// Create error if status code is not 200
if status != 200 {
collection.Error = strconv.Itoa(status)
e := errors.New("Status not 200")
return collection, status, e
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &tmp_single)
if err != nil {
err2 := json.Unmarshal(body, &tmp_multiple)
if err2 != nil {
fmt.Printf("Something very wrong. Can't create collection from json:\n1: %v \n 2: %v", err.Error, err2.Error)
status = 500
return collection, status, errors.New(strings.Join([]string{err.Error(), err2.Error()}, " "))
}
collection.Error = tmp_multiple.Error
collection.Status = tmp_multiple.Status
collection.Limit = tmp_multiple.Limit
collection.Offset = tmp_multiple.Offset
collection.Total_count = tmp_multiple.Total_count
collection.Data = tmp_multiple.Data
//fmt.Printf("Mutiple %+v\n", collection)
return collection, status, nil
} else {
nodes = append(nodes, tmp_single.Data)
collection.Error = tmp_multiple.Error
collection.Data = nodes
return collection, status, nil
}
//nodes = append(nodes, tmp.Data.([]Node)[0])
fmt.Printf("Why %+v\n", collection)
return collection, status, nil
}
示例11: TestClientRequest
func TestClientRequest(t *testing.T) {
Convey("Client Tests", t, func() {
Convey("->setPath()", func() {
url := &url.URL{Host: "test"}
Convey("should format properly", func() {
setPath(url, "tests")
So(url.String(), ShouldEqual, "//test/tests")
})
Convey("should respect an existing path", func() {
url.Path = "admin"
setPath(url, "test")
So(url.String(), ShouldEqual, "//test/admin/test")
})
})
Convey("->setIDPath()", func() {
url := &url.URL{Host: "test"}
Convey("should format properly an id url", func() {
setIDPath(url, "tests", "1")
So(url.String(), ShouldEqual, "//test/tests/1")
})
})
})
}
示例12: rewriteURL
// rewriteURL rewrites a single URL to go through the proxy, if the URL refers
// to the same host as sourceURL, which is the page on which the target URL
// occurred. If any error occurs (e.g. parsing), it returns targetURL.
func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL) string {
url, err := url.Parse(targetURL)
if err != nil {
return targetURL
}
isDifferentHost := url.Host != "" && url.Host != sourceURL.Host
isRelative := !strings.HasPrefix(url.Path, "/")
if isDifferentHost || isRelative {
return targetURL
}
url.Scheme = t.Scheme
url.Host = t.Host
origPath := url.Path
// Do not rewrite URL if the sourceURL already contains the necessary prefix.
if strings.HasPrefix(url.Path, t.PathPrepend) {
return url.String()
}
url.Path = path.Join(t.PathPrepend, url.Path)
if strings.HasSuffix(origPath, "/") {
// Add back the trailing slash, which was stripped by path.Join().
url.Path += "/"
}
return url.String()
}
示例13: refPage
func refPage(page interface{}, ref, methodName string) template.HTML {
value := reflect.ValueOf(page)
method := value.MethodByName(methodName)
if method.IsValid() && method.Type().NumIn() == 1 && method.Type().NumOut() == 2 {
result := method.Call([]reflect.Value{reflect.ValueOf(ref)})
url, err := result[0], result[1]
if !err.IsNil() {
jww.ERROR.Printf("%s", err.Interface())
return template.HTML(fmt.Sprintf("%s", err.Interface()))
}
if url.String() == "" {
jww.ERROR.Printf("ref %s could not be found\n", ref)
return template.HTML(ref)
}
return template.HTML(url.String())
}
jww.ERROR.Printf("Can only create references from Page and Node objects.")
return template.HTML(ref)
}
示例14: Test_ItTracksURLsItHasVisited
func Test_ItTracksURLsItHasVisited(t *testing.T) {
output := new(bytes.Buffer)
mockFetcher := &MockFetcher{}
app := NewCrawlerApp(output, mockFetcher)
urlString := "http://www.google.com"
expectedVisitedMap := map[string]bool{
urlString: true,
"http://www.google.com/1": true,
}
url, err := url.Parse(urlString)
assert.Nil(t, err)
mockFetcher.On("Fetch", url.String()).Return(&PageResults{
internalURLs: map[string]bool{
"http://www.google.com/1": true,
},
}, nil)
mockFetcher.On("Fetch", url.String()+"/1").Return(&PageResults{}, nil)
app.waitGroup.Add(1)
app.Crawl(url, 2)
app.waitGroup.Wait()
assert.Equal(t, expectedVisitedMap, app.Visited)
}
示例15: TestExtracRequestURL
func TestExtracRequestURL(t *testing.T) {
urlTests := []struct {
r *http.Request
url string
}{
{
&http.Request{
Host: "localhost",
URL: &url.URL{Path: "/path", RawQuery: "a=b"},
},
"http://localhost/path",
},
{
&http.Request{
Host: "www.myhost.com",
URL: &url.URL{Path: "/"},
TLS: &tls.ConnectionState{},
},
"https://www.myhost.com/",
},
}
for i, ut := range urlTests {
if url := extractRequestURL(ut.r); url.String() != ut.url {
t.Errorf("%d. extractRequestURL() = %q; want %q", i, url.String(), ut.url)
}
}
}