Golang Request.URL Examples

Golang Request.URL - 30 examples found. These are the top rated real world Golang examples of net/http.Request.URL extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func TestThreadList(t *testing.T) {
	ctl := gomock.NewController(t)
	defer ctl.Finish()

	threadServiceMock := service.NewMockThread(ctl)
	threadServiceMock.
		EXPECT().
		FindThreads(dao.Paging{Limit: 5, Offset: 0, OrderBy: "updated_at desc"}).
		Return(model.ThreadSlice{}, nil).
		Times(1)
	threadHandler := NewThread(threadServiceMock)

	ctx := context.Background()
	{
		r := http.Request{}
		url, err := url.Parse("http://localhost?limit=a&offset=0")
		if err != nil {
			t.Fatal(err)
		}
		r.URL = url
		res := threadHandler.List(ctx, &r)
		assert.Equal(t, response.BadRequest, res, "")
	}

	{
		r := http.Request{}
		url, err := url.Parse("http://localhost?limit=5&offset=0")
		if err != nil {
			t.Fatal(err)
		}
		r.URL = url
		threadHandler.List(ctx, &r)
	}
}
Example #2
0
func (c *Config) MapRequest(r *http.Request) {
	if mapped, err := c.MapURL(r.URL); err != nil {
		r.URL = &url.URL{Host: r.Host, Path: "/404", Scheme: "http"}
	} else {
		r.URL = &mapped
	}
}
Example #3
0
func makeRequest(params map[string]string) (*http.Request, error) {
	r := new(http.Request)
	r.Method = params["METHOD"]
	if r.Method == "" {
		return nil, errors.New("mongrel2: no METHOD")
	}

	r.Proto = params["VERSION"]
	var ok bool
	r.ProtoMajor, r.ProtoMinor, ok = http.ParseHTTPVersion(r.Proto)
	if !ok {
		return nil, errors.New("mongrel2: invalid protocol version")
	}

	r.Trailer = http.Header{}
	r.Header = http.Header{}

	r.Host = params["Host"]
	r.Header.Set("Referer", params["Referer"])
	r.Header.Set("User-Agent", params["User-Agent"])

	if lenstr := params["Content-Length"]; lenstr != "" {
		clen, err := strconv.ParseInt(lenstr, 10, 64)
		if err != nil {
			return nil, errors.New("mongrel2: bad Content-Length")
		}
		r.ContentLength = clen
	}

	for k, v := range params {
		if !skipHeader[k] {
			r.Header.Add(k, v)
		}
	}

	// TODO: cookies

	if r.Host != "" {
		url_, err := url.Parse("http://" + r.Host + params["URI"])
		if err != nil {
			return nil, errors.New("mongrel2: failed to parse host and URI into a URL")
		}
		r.URL = url_
	}
	if r.URL == nil {
		url_, err := url.Parse(params["URI"])
		if err != nil {
			return nil, errors.New("mongrel2: failed to parse URI into a URL")
		}
		r.URL = url_
	}

	// TODO: how do we know if we're using HTTPS?
	// TODO: fill in r.RemoteAddr

	return r, nil
}
Example #4
0
File: websocket.go Project: kr/webx
func (p *WebsocketProxy) Proxy(w http.ResponseWriter, r *http.Request) {
	hj, ok := w.(http.Hijacker)
	if !ok {
		log.Println("hijack assertion failed", r.Host, r.URL.Path)
		p.handler.ServeHTTP(w, r) // last-ditch effort as plain http
		return
	}
	conn, rw, err := hj.Hijack()
	if err != nil {
		log.Println("hijack failed", r.Host, r.URL.Path, err)
		p.handler.ServeHTTP(w, r) // last-ditch effort as plain http
		return
	}
	defer conn.Close()
	rw.Flush()

	wrapreq := new(http.Request)
	wrapreq.Proto = "HTTP/1.1"
	wrapreq.ProtoMajor, wrapreq.ProtoMinor = 1, 1
	wrapreq.Method = "WEBSOCKET"
	wrapreq.Host = r.Host
	const dummy = "/"
	wrapreq.URL = &url.URL{Path: dummy}
	var buf bytes.Buffer
	r.Write(&buf)
	wrapreq.Body = ioutil.NopCloser(io.MultiReader(&buf, conn))
	resp, err := p.transport.RoundTrip(wrapreq)
	if err != nil || resp.StatusCode != 200 {
		io.WriteString(conn, "HTTP/1.0 503 Gateway Failed\r\n")
		io.WriteString(conn, "Connection: close\r\n\r\n")
		return
	}
	defer resp.Body.Close()
	io.Copy(conn, resp.Body)
}
Example #5
0
func TestGetFormatForRequest(t *testing.T) {

	var request *http.Request

	request = new(http.Request)
	request.URL, _ = url.Parse(testDomain + "/people/123/groups/456.json")
	if getFormatForRequest(request) != JSON_FORMAT {
		t.Errorf("getFormatForRequest should be 'JSON' not '%s'", getFormatForRequest(request))
	}

	request.URL, _ = url.Parse(testDomain + "/people/123/groups/456.xml")
	if getFormatForRequest(request) != XML_FORMAT {
		t.Errorf("getFormatForRequest should be 'XML' not '%s'", getFormatForRequest(request))
	}

	request.URL, _ = url.Parse(testDomain + "/people/123/groups/456")
	if getFormatForRequest(request) != DEFAULT_FORMAT {
		t.Errorf("getFormatForRequest should be '%s' not '%s'", DEFAULT_FORMAT, getFormatForRequest(request))
	}

	request.URL, _ = url.Parse(testDomain + "/people/123/groups/456.html")
	if getFormatForRequest(request) != HTML_FORMAT {
		t.Errorf("getFormatForRequest should be '%s' not '%s'", HTML_FORMAT, getFormatForRequest(request))
	}
	request.URL, _ = url.Parse(testDomain + "/people/123/groups/456.htm")
	if getFormatForRequest(request) != HTML_FORMAT {
		t.Errorf("getFormatForRequest should be '%s' not '%s'", HTML_FORMAT, getFormatForRequest(request))
	}

	request.URL = nil
	if getFormatForRequest(request) != DEFAULT_FORMAT {
		t.Errorf("getFormatForRequest should be 'JSON' not '%s'", getFormatForRequest(request))
	}

}
Example #6
0
func (r Client) Do(l Logger, req *http.Request) (resp *http.Response, err error) {
	//check bind remote ip
	if r.BindRemoteIp != "" {
		oldReqUrl := req.URL.String()
		oldReqUri, _ := url.Parse(oldReqUrl)

		newUrlStr := fmt.Sprintf("%s://%s%s", oldReqUri.Scheme, r.BindRemoteIp, oldReqUri.RequestURI())
		newUrl, _ := url.Parse(newUrlStr)

		req.URL = newUrl
		req.Header.Set("Host", oldReqUri.Host)
	}

	if l != nil {
		req.Header.Set("X-Reqid", l.ReqId())
	}

	req.Header.Set("User-Agent", UserAgent)
	resp, err = r.Client.Do(req)
	if err != nil {
		return
	}

	if l != nil {
		details := resp.Header["X-Log"]
		if len(details) > 0 {
			l.Xput(details)
		}
	}
	return
}