Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chuan-yu/go-thrift-mock
A thrift mock server written in Go
https://github.com/chuan-yu/go-thrift-mock
Last synced: about 1 month ago
JSON representation
A thrift mock server written in Go
- Host: GitHub
- URL: https://github.com/chuan-yu/go-thrift-mock
- Owner: chuan-yu
- License: mit
- Created: 2021-02-07T06:00:15.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-02-21T14:36:20.000Z (almost 4 years ago)
- Last Synced: 2024-08-03T09:07:03.937Z (4 months ago)
- Language: Go
- Size: 2.25 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-star - go-thrift-mock - yu | 2 | (Go)
README
# GO-THRIFT-MOCK
![Test](https://github.com/chuan-yu/go-thrift-mock/actions/workflows/ci.yml/badge.svg)`go-thrift-mock` is a thrift mock server written in `Go`. It can return any mock thrift response without a predefined IDL.
This project is inspired by [thrift-mock](https://github.com/didi/thrift-mock) project.
## Intallation
```
go get https://github.com/chuan-yu/go-thrift-mock
```## Quick Start
### Start a mock server instance
```go
serverAddr := ":8888"
s := server.MustNewMockServer(serverAddr)go func() {
if err := s.Start(); err != nil {
fmt.Printf("failed to start server: %s", err.Error())
return
}
}()// wait for mock server to start properly
time.Sleep(1 * time.Second)
```
### Set expected returnAssume you have an IDL below.
```thrift
struct Request{
1:optional string msg,
}struct Response{
1:required i32 code,
2:required string responseMsg,
}service HelloService {
Response sayHello(1:required Request request);
}
```To set the `sayHello` method to return a mock `Response` instance:
```go
result := Response{
Code: 200,
ResponseMsg: "mock message",
}
expectedReturn := server.ExpectedReturn{
Response: &result,
}s.SetExpectedReturn("sayHello", expectedReturn)
```
Now when a client calls the server, the above mock response is returned.You can also mock an error response:
```go
expectedReturn = server.ExpectedReturn{
Err: errors.New("mock error"),
}
s.SetExpectedReturn("sayHello", expectedReturn)
```
### Stop the mock server
```go
s.Stop()
```## Example Code
See `example/main.go` for a working example