Skip to content

Commit

Permalink
Issue #4 investigation
Browse files Browse the repository at this point in the history
  • Loading branch information
johnny-debt committed Aug 15, 2018
1 parent 5408fc5 commit f5d337b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 57 deletions.
63 changes: 63 additions & 0 deletions instascrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package instascrap

import (
"errors"
"io"
"net/http"
)

// HTTPClientProvider is a factor
type HTTPClientProvider func() *http.Client

// Instascrap is main object which provides access to other functions
type Instascrap struct {
httpClientProvider HTTPClientProvider
}

// NewInstascrap instantiate Instascrap structure
func NewInstascrap(httpClientProvider HTTPClientProvider) Instascrap {
instascrap := Instascrap{}

if httpClientProvider != nil {
instascrap.httpClientProvider = httpClientProvider
} else {
instascrap.httpClientProvider = defaultHTTPClientProvider
}

return instascrap
}

// Provides default HTTP client
func defaultHTTPClientProvider() *http.Client {
return &http.Client{}
}

func (instascrap *Instascrap) getDataFromURL(URL string, reader func(r io.Reader) ([]byte, error)) ([]byte, error) {
// create a request
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
return nil, err
}
// use the http client to fetch the page
httpClient := instascrap.httpClientProvider()
if httpClient == nil {
return nil, errors.New("HTTP Client not available")
}
resp, err := httpClient.Do(req)

if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, errors.New("statusCode != 200")
}
defer resp.Body.Close()

body, err := reader(resp.Body)
if err != nil {
return nil, err
}

return body, nil
}
17 changes: 17 additions & 0 deletions instascrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@ package instascrap

import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestInstascrapDefaultCreation(t *testing.T) {
NewInstascrap(nil)
}

func getEmptyHTTPClient() *http.Client {
return nil
}
func TestCustomHTTPClientProvider(t *testing.T) {
instascrap := NewInstascrap(getEmptyHTTPClient)
_, error := instascrap.getDataFromURL("http://google.com", nil)
assert.Error(t, error)
}

func getTestDataPath() string {
pwd, _ := os.Getwd()
return filepath.Join(pwd, "test-data")
Expand Down
3 changes: 2 additions & 1 deletion mapping_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package instascrap

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetMediasFromHashtagPage(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type Media struct {
IsAdvertising bool
}

// Returns latest medias from the hashtag page
func GetHashtagMedia(tag string) ([]Media, error) {
// GetHashtagMedia returns latest medias from the hashtag page
func (instascrap *Instascrap) GetHashtagMedia(tag string) ([]Media, error) {
var medias []Media
url := fmt.Sprintf(hashtagMediasURL, tag, "")
jsonBody, err := getDataFromURL(url, ioutil.ReadAll)
jsonBody, err := instascrap.getDataFromURL(url, ioutil.ReadAll)
if err != nil {
return nil, err
}
Expand Down
27 changes: 15 additions & 12 deletions media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ package instascrap

import (
"fmt"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"testing"

"github.com/stretchr/testify/assert"
gock "gopkg.in/h2non/gock.v1"
)

// Ensures that this method returns exactly response body
func TestGetHashtagMediaSuccessful(t *testing.T) {
defer gock.Off()
hashtag := "something"
maxId := ""
maxID := ""
apiEndpoint := "https://www.instagram.com"
apiUri := fmt.Sprintf("explore/tags/%s", hashtag)
params := map[string]string{"__a": "1", "max_id": maxId}
apiURI := fmt.Sprintf("explore/tags/%s", hashtag)
params := map[string]string{"__a": "1", "max_id": maxID}

json := ReadTestDataFile("test-01-get-medias-from-hashtag-page.json")

gock.New(apiEndpoint).
Get(apiUri).
Get(apiURI).
MatchParams(params).
Reply(200).
JSON(json)

medias, err := GetHashtagMedia(hashtag)
instascrap := NewInstascrap(nil)
medias, err := instascrap.GetHashtagMedia(hashtag)

assert.NoError(t, err)
assert.Len(t, medias, 63)
Expand All @@ -35,18 +37,19 @@ func TestGetHashtagMediaSuccessful(t *testing.T) {
func TestGetHashtagMediaJSONRetrievingError(t *testing.T) {
defer gock.Off()
hashtag := "something"
maxId := ""
maxID := ""
apiEndpoint := "https://www.instagram.com"
apiUri := fmt.Sprintf("explore/tags/%s", hashtag)
params := map[string]string{"__a": "1", "max_id": maxId}
apiURI := fmt.Sprintf("explore/tags/%s", hashtag)
params := map[string]string{"__a": "1", "max_id": maxID}

gock.New(apiEndpoint).
Get(apiUri).
Get(apiURI).
MatchParams(params).
Reply(201).
JSON("")

medias, err := GetHashtagMedia(hashtag)
instascrap := NewInstascrap(nil)
medias, err := instascrap.GetHashtagMedia(hashtag)

assert.Error(t, err)
assert.Nil(t, medias)
Expand Down
27 changes: 0 additions & 27 deletions utils.go

This file was deleted.

33 changes: 19 additions & 14 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ package instascrap

import (
"errors"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"io"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
gock "gopkg.in/h2non/gock.v1"
)

// Ensures that this method returns exactly response body
func TestGetDataFromUrlSuccessful(t *testing.T) {
defer gock.Off()

apiUrl := "https://example.com"
apiURL := "https://example.com"
apiPath := "status"
expectedResponse := "anything"

gock.New(apiUrl).
gock.New(apiURL).
Get(apiPath).
Reply(200).
BodyString(expectedResponse)

actualResponse, err := getDataFromURL(apiUrl+"/"+apiPath, ioutil.ReadAll)
instascrap := NewInstascrap(nil)
actualResponse, err := instascrap.getDataFromURL(apiURL+"/"+apiPath, ioutil.ReadAll)

assert.Equal(t, []byte(expectedResponse), actualResponse)
assert.NoError(t, err)
Expand All @@ -32,15 +34,16 @@ func TestGetDataFromUrlSuccessful(t *testing.T) {
func TestGetDataFromUrlError(t *testing.T) {
defer gock.Off()

apiUrl := "http://example.com"
apiURL := "http://example.com"
apiPath := "status"

gock.New(apiUrl).
gock.New(apiURL).
Get(apiPath).
Reply(302).
BodyString("")

_, err := getDataFromURL(apiUrl+"/"+apiPath, ioutil.ReadAll)
instascrap := NewInstascrap(nil)
_, err := instascrap.getDataFromURL(apiURL+"/"+apiPath, ioutil.ReadAll)

assert.Error(t, err)
}
Expand All @@ -49,15 +52,16 @@ func TestGetDataFromUrlError(t *testing.T) {
func TestGetDataFromUrlNon200HttpCode(t *testing.T) {
defer gock.Off()

apiUrl := "http://example.com"
apiURL := "http://example.com"
apiPath := "status"

gock.New(apiUrl).
gock.New(apiURL).
Get(apiPath).
Reply(201).
BodyString("")

_, err := getDataFromURL(apiUrl+"/"+apiPath, ioutil.ReadAll)
instascrap := NewInstascrap(nil)
_, err := instascrap.getDataFromURL(apiURL+"/"+apiPath, ioutil.ReadAll)

assert.Error(t, err)
}
Expand All @@ -66,15 +70,16 @@ func TestGetDataFromUrlNon200HttpCode(t *testing.T) {
func TestGetDataFromUrlBodyReadError(t *testing.T) {
defer gock.Off()

apiUrl := "http://example.com"
apiURL := "http://example.com"
apiPath := "status"

gock.New(apiUrl).
gock.New(apiURL).
Get(apiPath).
Reply(200).
BodyString("")

_, err := getDataFromURL(apiUrl+"/"+apiPath, func(r io.Reader) ([]byte, error) {
instascrap := NewInstascrap(nil)
_, err := instascrap.getDataFromURL(apiURL+"/"+apiPath, func(r io.Reader) ([]byte, error) {
return nil, errors.New("IO Reader error occurred")
})

Expand Down

0 comments on commit f5d337b

Please sign in to comment.