Adds the ability to collect BitcoinTalk.org forum threads, user profiles, and search results. This change introduces a new `collect bitcointalk` command with three subcommands: - `thread`: Archives a full multi-page thread to a Markdown file. - `user`: Saves a user's profile to a JSON file. - `search`: Saves search results to a JSON file. Key features: - Handles multi-page threads. - Implements rate limiting to avoid being blocked. - Includes unit and integration tests with a mock HTTP server and embedded test data. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
35 lines
953 B
Go
35 lines
953 B
Go
package mocks
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
// MockRoundTripper is a mock implementation of http.RoundTripper.
|
|
type MockRoundTripper struct {
|
|
responses map[string]*http.Response
|
|
}
|
|
|
|
// RoundTrip implements the http.RoundTripper interface.
|
|
func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
if res, ok := m.responses[req.URL.String()]; ok {
|
|
// Make a copy of the response so it can be read multiple times
|
|
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
|
res.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
|
|
return res, nil
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusNotFound,
|
|
Body: ioutil.NopCloser(bytes.NewBufferString("not found")),
|
|
}, nil
|
|
}
|
|
|
|
// NewMockClient returns a mock HTTP client that returns the given responses.
|
|
func NewMockClient(responses map[string]*http.Response) *http.Client {
|
|
return &http.Client{
|
|
Transport: &MockRoundTripper{
|
|
responses: responses,
|
|
},
|
|
}
|
|
}
|