Documentation
¶
Overview ¶
Package mocks는 fetcher 패키지의 테스트를 위한 Mock 구현체들을 제공합니다.
Mock 구현체 선택 가이드 ¶
이 패키지는 두 가지 주요 Mock 구현체를 제공합니다:
1. MockFetcher (testify/mock 기반)
- 용도: 정교한 Mock 검증이 필요한 단위 테스트
- 장점: 메서드 호출 횟수, 인자 검증 등 상세한 Mock 동작 제어 가능
- 단점: 설정이 다소 복잡함
- 사용 예: mockFetcher := mocks.NewMockFetcher() mockFetcher.On("Get", mock.Anything, "https://example.com").Return(resp, nil)
2. MockHTTPFetcher (수동 구현)
- 용도: 통합 테스트, 벤치마크, 복잡한 시나리오 시뮬레이션
- 장점: URL별 응답/에러/지연 설정 가능, 간단한 설정, Thread-Safe
- 단점: 호출 검증 기능이 제한적 (기본적인 호출 횟수 확인 등은 지원)
- 사용 예: mockFetcher := mocks.NewMockHTTPFetcher() mockFetcher.SetResponse("https://example.com", []byte("response")) mockFetcher.SetDelay("https://slow.com", 100*time.Millisecond)
동시성 안전성 ¶
- MockFetcher: testify/mock 패키지가 내부적으로 동시성을 처리합니다.
- MockHTTPFetcher: sync.Mutex를 사용하여 완벽한 동시성 안전성을 보장합니다.
- MockReadCloser: atomic 연산을 사용하여 Close() 호출에 대한 동시성 안전성을 보장합니다.
Index ¶
- func NewMockResponse(body string, statusCode int) *http.Response
- func NewMockResponseWithJSON(jsonBody string, statusCode int) *http.Response
- type MockFetcher
- type MockHTTPFetcher
- func (m *MockHTTPFetcher) Close() error
- func (m *MockHTTPFetcher) Do(req *http.Request) (*http.Response, error)
- func (m *MockHTTPFetcher) GetCallCount(url string) int
- func (m *MockHTTPFetcher) GetRequestedURLs() []string
- func (m *MockHTTPFetcher) GetRequests() []RequestRecord
- func (m *MockHTTPFetcher) Reset()
- func (m *MockHTTPFetcher) SetDelay(url string, d time.Duration)
- func (m *MockHTTPFetcher) SetError(url string, err error)
- func (m *MockHTTPFetcher) SetHeader(url string, key, value string)
- func (m *MockHTTPFetcher) SetResponse(url string, body []byte)
- func (m *MockHTTPFetcher) SetResponseWithStatus(url string, body []byte, statusCode int)
- type MockReadCloser
- type RequestRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMockResponse ¶
NewMockResponse 주어진 body와 status code를 가진 새로운 http.Response를 생성합니다.
이 함수는 테스트에서 간단한 HTTP 응답을 생성할 때 사용됩니다. Body는 io.NopCloser로 래핑되어 Close() 호출 시 아무 동작도 하지 않습니다.
Types ¶
type MockFetcher ¶
MockFetcher Fetcher 인터페이스의 Mock 구현체 (Testify 사용)
func NewMockFetcher ¶
func NewMockFetcher() *MockFetcher
NewMockFetcher 새로운 MockFetcher 인스턴스를 생성합니다.
func (*MockFetcher) Close ¶
func (m *MockFetcher) Close() error
type MockHTTPFetcher ¶
type MockHTTPFetcher struct {
// contains filtered or unexported fields
}
MockHTTPFetcher 테스트용 Mock Fetcher (sync.Mutex 기반) 복잡한 동작(응답 지연, 에러 주입, 상태 코드 등)을 시뮬레이션하기 위해 사용됩니다.
func NewMockHTTPFetcher ¶
func NewMockHTTPFetcher() *MockHTTPFetcher
NewMockHTTPFetcher 새로운 MockHTTPFetcher를 생성합니다.
func (*MockHTTPFetcher) Close ¶
func (m *MockHTTPFetcher) Close() error
func (*MockHTTPFetcher) Do ¶
Do Mock HTTP 요청을 수행합니다.
기능: - Context 취소 감지 - 요청 상세 정보 기록 (Method, URL, Header, Body) - 설정된 지연(Delay) 시뮬레이션 - 설정된 에러 반환 - 설정된 응답(Body, Status, Header) 반환
func (*MockHTTPFetcher) GetCallCount ¶
func (m *MockHTTPFetcher) GetCallCount(url string) int
GetCallCount 특정 URL이 호출된 횟수를 반환합니다.
func (*MockHTTPFetcher) GetRequestedURLs ¶
func (m *MockHTTPFetcher) GetRequestedURLs() []string
GetRequestedURLs 요청된 URL 목록을 반환합니다. (호환성을 위해 유지)
func (*MockHTTPFetcher) GetRequests ¶
func (m *MockHTTPFetcher) GetRequests() []RequestRecord
GetRequests 기록된 모든 요청 상세 정보를 반환합니다.
func (*MockHTTPFetcher) SetDelay ¶
func (m *MockHTTPFetcher) SetDelay(url string, d time.Duration)
SetDelay 특정 URL 요청 시 응답 지연 시간을 설정합니다.
func (*MockHTTPFetcher) SetError ¶
func (m *MockHTTPFetcher) SetError(url string, err error)
SetError 특정 URL에 대한 에러를 설정합니다.
func (*MockHTTPFetcher) SetHeader ¶
func (m *MockHTTPFetcher) SetHeader(url string, key, value string)
SetHeader 특정 URL 응답에 헤더를 설정합니다. SetResponse나 SetResponseWithStatus가 먼저 호출되어 있어야 합니다. (호출되지 않았다면 기본 200 OK 응답으로 초기화된 후 헤더가 설정됩니다)
func (*MockHTTPFetcher) SetResponse ¶
func (m *MockHTTPFetcher) SetResponse(url string, body []byte)
SetResponse 특정 URL에 대한 성공 응답(200 OK)을 설정합니다.
func (*MockHTTPFetcher) SetResponseWithStatus ¶
func (m *MockHTTPFetcher) SetResponseWithStatus(url string, body []byte, statusCode int)
SetResponseWithStatus 특정 URL에 대한 응답 Body와 Status Code를 설정합니다.
type MockReadCloser ¶
type MockReadCloser struct {
Reader *bytes.Reader
// CloseErr 설정 시 Close() 호출에서 이 에러를 반환합니다.
CloseErr error
// ReadErr 설정 시 Read() 호출에서 이 에러를 반환합니다.
ReadErr error
// contains filtered or unexported fields
}
MockReadCloser io.ReadCloser 인터페이스를 구현하며, Close() 호출 여부를 추적합니다. 또한 Close 시 에러 반환을 시뮬레이션할 수 있습니다.
func NewMockReadCloser ¶
func NewMockReadCloser(data string) *MockReadCloser
NewMockReadCloser 문자열 데이터를 가진 MockReadCloser를 생성합니다.
func NewMockReadCloserBytes ¶
func NewMockReadCloserBytes(data []byte) *MockReadCloser
NewMockReadCloserBytes 바이트 슬라이스 데이터를 가진 MockReadCloser를 생성합니다.
func (*MockReadCloser) Close ¶
func (m *MockReadCloser) Close() error
func (*MockReadCloser) GetCloseCount ¶
func (m *MockReadCloser) GetCloseCount() int64
GetCloseCount Close() 메서드가 호출된 횟수를 반환합니다.
func (*MockReadCloser) WasRead ¶
func (m *MockReadCloser) WasRead() bool
WasRead Read() 메서드가 한 번이라도 호출되었는지 여부를 반환합니다.