本文整理汇总了Golang中net/http.Get函数的典型用法代码示例。如果您正苦于以下问题:Golang Get函数的具体用法?Golang Get怎么用?Golang Get使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Get函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestWithRoutes
func TestWithRoutes(t *testing.T) {
t.Parallel()
assert := assert.New(t)
rtes := sampleRoutes()
server := mockserver.WithRoutes(rtes)
defer server.Close()
// Check 404 route
resp, err := http.Get(server.URL + "/random")
assert.NoError(err)
assert.Equal(404, resp.StatusCode)
// check POST
resp, err = http.Post(server.URL+"/welcome/tony", "plain/text", bytes.NewBuffer([]byte{}))
assert.NoError(err)
assert.Equal(200, resp.StatusCode)
// check body response
body, err := parseResp(resp.Body)
assert.NoError(err)
assert.Equal(body, "Welcome tony")
// check 405
resp, err = http.Get(server.URL + "/welcome/tony")
assert.NoError(err)
assert.Equal(405, resp.StatusCode)
// check second route
resp, err = http.Get(server.URL + "/hello/world")
assert.NoError(err)
assert.Equal(200, resp.StatusCode)
// check hello world
body, err = parseResp(resp.Body)
assert.NoError(err)
assert.Equal("Hello world", body)
}
示例2: loadRemoteFeedList
func loadRemoteFeedList(input string) []string {
feeds := []string{}
switch path.Ext(input) {
case ".opml":
opml := OPML{}
resp, err := http.Get(input)
if err != nil {
logger.Fatal("couldn't load feed list: %v", err)
}
decoder := xml.NewDecoder(resp.Body)
decoder.CharsetReader = charset.NewReader
decoder.Decode(&opml)
for _, outline := range opml.Body.Outlines {
if outline.XmlUrl != "" {
feeds = append(feeds, outline.XmlUrl)
}
}
case ".txt", ".yaml":
resp, err := http.Get(input)
if err != nil {
logger.Fatal("couldn't load feed list: %v", err)
}
data, _ := ioutil.ReadAll(resp.Body)
yaml.Unmarshal(data, &feeds)
}
return feeds
}
示例3: TestGetContact
func TestGetContact(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(addressBookHandler))
defer ts.Close()
contacts = []contact.Contact{
contact.Contact{
Name: "Kotchaphan",
Tel: "000",
Email: "[email protected]",
},
}
resp, _ := http.Get(ts.URL + "/contacts?name=Kotchaphan")
if resp.StatusCode != http.StatusOK {
t.Error("Expect status is 200")
}
b, _ := ioutil.ReadAll(resp.Body)
if string(b) != `{"name":"Kotchaphan","tel":"000","email":"[email protected]"}`+"\n" {
t.Error(`Expect json {"name" : "Kotchaphan" , "tel" : "000" , "email" : "[email protected]"} but got `, string(b))
}
resp, _ = http.Get(ts.URL + "/contacts?name=haha")
if resp.StatusCode != http.StatusNotFound {
t.Error("Expect status code is 404")
}
}
示例4: Test_Routing
func Test_Routing(t *testing.T) {
a := assert.New(t)
ts := httptest.NewServer(NewMux())
defer ts.Close()
res, err := http.Get(ts.URL + "/users")
a.NoError(err)
body, err := ioutil.ReadAll(res.Body)
a.Equal(string(body), "Users Index")
res, err = http.Get(ts.URL + "/users/42")
a.NoError(err)
body, err = ioutil.ReadAll(res.Body)
a.Equal(string(body), "Users Show: 42")
res, err = http.Post(ts.URL+"/users/", "plain/text", strings.NewReader("Hello!"))
a.NoError(err)
body, err = ioutil.ReadAll(res.Body)
a.Equal(string(body), "Users Create: Hello!")
res, err = http.Get(ts.URL + "/posts")
a.NoError(err)
body, err = ioutil.ReadAll(res.Body)
a.Equal(string(body), "POSTS!")
}
示例5: TestAPIGroupMissing
func TestAPIGroupMissing(t *testing.T) {
indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
handler := &apiGroupHandler{
lister: listers.NewAPIServiceLister(indexer),
groupName: "foo",
}
server := httptest.NewServer(handler)
defer server.Close()
resp, err := http.Get(server.URL + "/apis/groupName/foo")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("expected %v, got %v", resp.StatusCode, http.StatusNotFound)
}
// foo still has no api services for it (like it was deleted), it should 404
resp, err = http.Get(server.URL + "/apis/groupName/")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("expected %v, got %v", resp.StatusCode, http.StatusNotFound)
}
}
示例6: fetch
func fetch() []string {
var server string = "https://raw.githubusercontent.com/roypur/hosts/master/src"
resp, _ := http.Get(server)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
src := strings.Split(strings.TrimSpace(string(body)), "\n")
requestData := []string{}
var j int = len(src) - 1
for i := 0; i <= j; i++ {
resp, _ := http.Get(src[i])
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
requestData = append(requestData, string(body))
}
var appendString string = strings.Join(requestData, "\n")
retVal := strings.Split(appendString, "\n")
return retVal
}
示例7: TestServerData
func TestServerData(t *testing.T) {
s := startTestServer(t)
s.putData("hello", []byte("world"))
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/hello", s.port))
if err != nil {
t.Errorf("failed to get 'hello': %v", err)
}
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Errorf("error while reading data: %v", err)
}
if string(b) != "world" {
t.Errorf("expected %q, got %q", "world", b)
}
s.rmData("hello")
resp, err = http.Get(fmt.Sprintf("http://localhost:%d/hello", s.port))
if err != nil {
t.Errorf("unexpected error requesting 'hello': %v", err)
}
if resp.StatusCode == http.StatusOK {
t.Errorf("expected non-OK status after removing 'hello'")
}
s.s.Stop()
}
示例8: TestMain
func TestMain(t *testing.T) {
mux := NewServer()
ts := httptest.NewServer(mux)
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
res, err = http.Get(ts.URL + "/A")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
res, err = http.Get(ts.URL + "/A/B")
if err != nil {
t.Fatal(err)
}
res, err = http.Get(ts.URL + "/A/B/URL")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusOK, res.StatusCode)
}
示例9: main
func main() {
r, err := http.Get("http://mattfarina.com")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer r.Body.Close()
o, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(o))
r2, err := http.Get("http://mattfarina.com/about")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer r2.Body.Close()
o, err = ioutil.ReadAll(r2.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(o))
}
示例10: TestMockTransport
func TestMockTransport(t *testing.T) {
Activate(&http.DefaultTransport)
defer Deactivate()
url := "https://github.com/"
body := "hello world"
RegisterResponder("GET", url, NewStringResponder(200, body))
resp, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(data) != body {
t.FailNow()
}
// the http client wraps our NoResponderFound error, so we just try and match on text
if _, err := http.Get(testUrl); !strings.Contains(err.Error(),
NoResponderFound.Error()) {
t.Fatal(err)
}
}
示例11: TestMockTransportNoResponder
func TestMockTransportNoResponder(t *testing.T) {
Activate(&http.DefaultTransport)
defer DeactivateAndReset()
Reset()
if DefaultTransport.noResponder != nil {
t.Fatal("expected noResponder to be nil")
}
if _, err := http.Get(testUrl); err == nil {
t.Fatal("expected to receive a connection error due to lack of responders")
}
RegisterNoResponder(NewStringResponder(200, "hello world"))
resp, err := http.Get(testUrl)
if err != nil {
t.Fatal("expected request to succeed")
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(data) != "hello world" {
t.Fatal("expected body to be 'hello world'")
}
}
示例12: TestServerGracefulStopKeepAlive
func TestServerGracefulStopKeepAlive(t *testing.T) {
s := NewServer(
"127.0.0.1:0",
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}),
)
l, err := net.Listen("tcp", s.Addr)
if nil != err {
t.Fatal(err)
}
go s.Serve(l)
if _, err := http.Get(fmt.Sprintf("http://%s", l.Addr())); nil != err {
t.Fatal(err)
}
then := time.Now()
s.Close()
now := time.Now()
if then.Add(510 * time.Millisecond).Before(now) {
t.Fatal("connection not closed quickly")
}
if _, err := http.Get(fmt.Sprintf("http://%s", l.Addr())); nil == err {
t.Fatal("GET / should have failed after server stopped")
}
}
示例13: TestFlashes
func TestFlashes(t *testing.T) {
resp, err := http.Get("http://localhost:8082/test/flash/")
if err != nil {
t.Errorf("%s", err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("%s", err.Error())
}
if string(body) != "static" {
t.Errorf("Body:%s:", body)
}
resp, err = http.Get("http://localhost:8082/test/flash/")
if err != nil {
t.Errorf("%s", err.Error())
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("%s", err.Error())
}
if string(body) != "static" {
t.Errorf("Body:%s:", body)
}
}
示例14: TestCount
// Testailee vain latautuuko sivu.
func TestCount(t *testing.T) {
var res *http.Response
var got []byte
var err error
ts := httptest.NewServer(http.DefaultServeMux)
defer ts.Close()
for i := 0; i < 10170; i++ {
res, _ := http.Get(ts.URL + "/count")
res.Body.Close()
}
if res, err = http.Get(ts.URL + "/count"); err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Error("Status code:", res.StatusCode)
}
if got, err = ioutil.ReadAll(res.Body); err != nil {
t.Fatal(err)
}
if !strings.Contains(string(got), "Laskuri: 10171") {
t.Fatalf("Expected count to be 10171. This is what we got:\n%s", string(got))
}
}
示例15: fetchEdges
func fetchEdges(offset int, limit int, channel chan<- []Edge) {
var edges []Edge
done, url := false, fmt.Sprintf(
"http://localhost:8080/api/graph?offset=%d&limit=%d",
offset, limit)
for resp, err := http.Get(url); !done; resp, err = http.Get(url) {
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
done = func() bool {
defer resp.Body.Close()
if resp.StatusCode == 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
err = json.Unmarshal(body, &edges)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
channel <- edges
}
return resp.StatusCode == 200
}()
}
}