2021-04-08 12:16:33 +02:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
2021-04-25 12:18:31 +02:00
|
|
|
"brainbaking.com/go-jamming/rest"
|
2021-04-11 09:50:27 +02:00
|
|
|
"encoding/json"
|
2021-04-23 09:39:49 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-04-08 16:37:04 +02:00
|
|
|
"io/ioutil"
|
2021-04-08 12:16:33 +02:00
|
|
|
"net/http"
|
2021-04-11 09:50:27 +02:00
|
|
|
"net/url"
|
2021-04-09 21:00:54 +02:00
|
|
|
"strings"
|
2021-04-08 12:16:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// neat trick! https://medium.com/@matryer/meet-moq-easily-mock-interfaces-in-go-476444187d10
|
|
|
|
type RestClientMock struct {
|
2021-04-11 09:50:27 +02:00
|
|
|
GetFunc func(string) (*http.Response, error)
|
|
|
|
GetBodyFunc func(string) (http.Header, string, error)
|
|
|
|
PostFunc func(string, string, string) error
|
|
|
|
PostFormFunc func(string, url.Values) error
|
2021-04-08 12:16:33 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 21:00:54 +02:00
|
|
|
// although these are still requied to match the rest.Client interface.
|
2021-04-08 12:16:33 +02:00
|
|
|
func (m *RestClientMock) Get(url string) (*http.Response, error) {
|
|
|
|
return m.GetFunc(url)
|
|
|
|
}
|
2021-04-11 09:50:27 +02:00
|
|
|
func (m *RestClientMock) GetBody(url string) (http.Header, string, error) {
|
2021-04-08 12:16:33 +02:00
|
|
|
return m.GetBodyFunc(url)
|
|
|
|
}
|
|
|
|
|
2021-04-11 09:50:27 +02:00
|
|
|
func (client *RestClientMock) PostForm(url string, formData url.Values) error {
|
|
|
|
return client.PostFormFunc(url, formData)
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:59:12 +02:00
|
|
|
func (m *RestClientMock) Post(url string, contentType string, body string) error {
|
|
|
|
return m.PostFunc(url, contentType, body)
|
|
|
|
}
|
|
|
|
|
2021-04-11 09:50:27 +02:00
|
|
|
func toHttpHeader(header map[string]interface{}) http.Header {
|
|
|
|
httpHeader := http.Header{}
|
|
|
|
for key, value := range header {
|
2021-04-25 12:18:31 +02:00
|
|
|
if key == "link1" || key == "link2" {
|
|
|
|
key = "link"
|
|
|
|
}
|
2021-04-11 09:50:27 +02:00
|
|
|
httpHeader.Add(key, value.(string))
|
|
|
|
}
|
|
|
|
return httpHeader
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:39:49 +02:00
|
|
|
func RelPathGetBodyFunc(relPath string) func(string) (http.Header, string, error) {
|
2021-04-11 09:50:27 +02:00
|
|
|
return func(url string) (http.Header, string, error) {
|
2021-04-23 09:39:49 +02:00
|
|
|
log.Debug().Str("url", url).Msg(" - GET call")
|
2021-04-09 10:12:14 +02:00
|
|
|
// url: https://brainbaking.com/something-something.html
|
|
|
|
// want: ../../mocks/something-something.html
|
2021-04-09 14:21:25 +02:00
|
|
|
mockfile := relPath + strings.ReplaceAll(url, "https://brainbaking.com/", "")
|
2021-04-09 10:12:14 +02:00
|
|
|
html, err := ioutil.ReadFile(mockfile)
|
|
|
|
if err != nil {
|
2021-04-19 21:38:53 +02:00
|
|
|
return nil, "", err
|
2021-04-09 10:12:14 +02:00
|
|
|
}
|
2021-04-11 09:50:27 +02:00
|
|
|
|
|
|
|
headerData, headerFileErr := ioutil.ReadFile(strings.ReplaceAll(mockfile, ".html", "-headers.json"))
|
|
|
|
if headerFileErr != nil {
|
2021-04-25 12:18:31 +02:00
|
|
|
header := http.Header{}
|
|
|
|
header.Set(rest.RequestUrl, url) // mimic actual implementation to track possible redirects
|
|
|
|
return header, string(html), nil
|
2021-04-11 09:50:27 +02:00
|
|
|
}
|
|
|
|
headerJson := map[string]interface{}{}
|
|
|
|
json.Unmarshal(headerData, &headerJson)
|
|
|
|
|
2021-04-25 12:18:31 +02:00
|
|
|
header := toHttpHeader(headerJson)
|
|
|
|
header.Set(rest.RequestUrl, url) // mimic actual implementation to track possible redirects
|
|
|
|
return header, string(html), nil
|
2021-04-08 16:37:04 +02:00
|
|
|
}
|
|
|
|
}
|