Skip to content

Instantly share code, notes, and snippets.

@s1moe2
Last active April 12, 2020 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1moe2/dea56787fbded26e5a32702a0bd9c451 to your computer and use it in GitHub Desktop.
Save s1moe2/dea56787fbded26e5a32702a0bd9c451 to your computer and use it in GitHub Desktop.
Golearnings: HTTP Requests
func GetThings() {
res, err := http.Get("https://httpbin.org/get")
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
log.Println(string(body))
}
func GetThings() {
const GetTimeout = 5
req, err := http.NewRequest(http.MethodGet, "https://httpbin.org/get", nil)
if err != nil {
log.Fatalln(err)
}
client := http.Client{
Timeout: GetTimeout * time.Second,
}
res, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
log.Println(string(body))
}
req, err := http.NewRequest(http.MethodGet, "https://httpbin.org/get", nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
type BinSlideshow struct {
Slideshow struct {
Author string `json:"author"`
Title string `json:"title"`
Slides []struct {
Title string `json:"title"`
} `json:"slides"`
} `json:"slideshow"`
}
func GetSlideshow() (*BinSlideshow, error) {
const GetTimeout = 5
req, err := http.NewRequest(http.MethodGet, "https://httpbin.org/json", nil)
if err != nil {
return nil, err
}
client := http.Client{
Timeout: GetTimeout * time.Second,
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
slideshow := &BinSlideshow{}
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&slideshow)
if err != nil {
return nil, err
}
return slideshow, nil
}
func DownloadSlideshow() error {
const GetTimeout = 5
req, err := http.NewRequest(http.MethodGet, "https://httpbin.org/json", nil)
if err != nil {
return err
}
client := http.Client{
Timeout: GetTimeout * time.Second,
}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
out, err := os.Create("res.json")
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, res.Body)
return err
}
package main
import (
"encoding/json"
"net/http"
)
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type Application struct {
httpClient HTTPClient
}
type BinSlideshow struct {
Slideshow struct {
Author string `json:"author"`
Title string `json:"title"`
Slides []struct {
Title string `json:"title"`
} `json:"slides"`
} `json:"slideshow"`
}
func (a *Application) GetSlideshow() (*BinSlideshow, error) {
const GetTimeout = 5
req, err := http.NewRequest(http.MethodGet, "https://httpbin.org/json", nil)
if err != nil {
return nil, err
}
res, err := a.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
slideshow := &BinSlideshow{}
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&slideshow)
if err != nil {
return nil, err
}
return slideshow, nil
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
type mockClient struct {
StubDo func(req *http.Request) (*http.Response, error)
}
func (m *mockClient) Do(req *http.Request) (*http.Response, error) {
return m.StubDo(req)
}
func TestGetSlideshow(t *testing.T) {
client := &mockClient{}
app := &Application{
httpClient: client,
}
t.Run("Expect error when httpbin is down", func(t *testing.T) {
client.StubDo = func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Body: ioutil.NopCloser(bytes.NewReader([]byte{})),
}, nil
}
_, err := app.GetSlideshow()
if err == nil {
t.Error("Expected error on GetSlideshow")
}
})
t.Run("Expect to get slideshow from httpbin", func(t *testing.T) {
body := BinSlideshow{}
bodyJson, _ := json.Marshal(body)
client.StubDo = func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(bodyJson)),
}, nil
}
_, err := app.GetSlideshow()
if err != nil {
fmt.Println(err)
t.Error("Unexpected error on GetSlideshow")
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment