Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
github.com/yuluobin/gin-test-mock-server
This is a simple web API mocking server based on Gin. Simply modify configuration file and map API requests and responses in YAML, you are ready to test front-end API without an actual server.
If you haven't modify or create your new config file, the program will load debug.yml
as default configuration file.
cd $GOPATH
go get -u -v -d github.com/yuluobin/Gin-Test-Mock-Server/...
cd $GOPATH/src/github.com/yuluobin/Gin-Test-Mock-Server
git checkout feat
sudo docker build -t mockserver . # Build docker image
sudo docker run -p 8081:8081 mockserver
In this way the mock server will run in docker. But if you want to build it in your own machine and start to work instantly, simply do as follows after checking out the feat
branch:
go build -o mockserver; ./mockserver
If you see Listening and serving HTTP on :8081
, the mock server is successfully running in the background.
This simple guide will help you go through the process of using the mock server. Make sure you have stopped the original mock server before continue.
Create a configuration file wherever you like (we recommend it be in the mockServer
directory). You can find the configuration format guide below. If you are still confused about how to write a YAML file, please refer to the default file debug.yml
.
Replace the default file name debug.yml
with your new config file in mockServer/system/config.go
.
Build the mockServer in docker and run it
sudo docker build -t mockserver . # Build docker image
sudo docker run -p 8081:8081 mockserver
Make sure the first port number is the same as the one in your configuration file.
Or of course you can run it without docker:
go build -o mockserver
./mockserver
Use curl or other service test methods to test your API.
(Don't know how to use curl? You can refer to the test examples below.)
(Optional) There's a lua scripts provided to test mock server performance using wrk. Test URIs should be placed in test_uri.txt
file. Since POST request need request body, all POST URIs should be placed after GET URIs, and request body must be in the separate line with its URI. For example:
/login?user=chadli&pwd=123456 # 1st GET URI
/login?user=ekopei&pwd=qwerty # 2nd GET URI
/set_userinfo # 1st POST URI
token=ABC&age=20 # 1st POST URI's request body
/set_userinfo # 2nd POST URI
token=DEF&age=21 # 2nd POST URI's request body
Make sure the mock server is running in the background before testing. For example:
wrk -t5 -c400 -d20s -s wrk_test.lua http://localhost:8081
(Don't know wrk? Check the link below https://github.com/wg/wrk).
The format of configuration file is YAML.
route
: the basic URI of this requestmethod
: the http request method (up to now the mock server supports GET
and POST
request)res
: is a respond array containing specific configuration of each request
uri
: a URI contains variablesheader
: format of respond messagepost_body
: contains post body. Can be omitted in GET
requestret_body
: contains respond message body if the request is successfulerr_body
: contains error message if the request is unsuccessful.As the default configuration file
$ curl "http://localhost:8081/login?user=chadli&pwd=123456"
{"msg":"Successfully logged in!","token":"ABC"}
$ curl "http://localhost:8081/login?pwd=qwerty&user=ekopei"
{"msg":"Successfully logged in!","token":"DEF"}
$ curl "http://localhost:8081/get_userinfo?token=ABC"
{"age":20,"gender":"male","msg":"Successfully get user info!"}
$ curl "http://localhost:8081/get_userinfo?token=DEF"
{"age":21,"gender":"male","msg":"Successfully get user info!"}
$ curl "http://localhost:8081/set_userinfo" -X POST -d 'token=ABC&age=20'
{"msg":"Successfully set user info!","ret_code":0}
$ curl "http://localhost:8081/set_userinfo" -X POST -d 'token=DEF&age=21'
{"msg":"Successfully set user info!","ret_code":0}
$ curl "http://localhost:8081/get_userinfo?token=ABD" # Wrong token
null
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.