我想要实现的是模拟谷歌oauth2端点的响应。下面是我的设置:
# docker-compose.yml
version: '3.8'
services:
busybox:
image: yauritux/busybox-curl:latest
command: tail -f /dev/null
networks:
- our-network
api-mock:
image: mockserver/mockserver
networks:
our-network:
aliases:
- oauth2.googleapis.com
environment:
MOCKSERVER_INITIALIZATION_JSON_PATH: /api-mock/expectations_init.json
MOCKSERVER_WATCH_INITIALIZATION_JSON: 'true'
volumes:
- ./api-mock/:/api-mock
ports:
- 1080:1080
networks:
our-network:
我们对Mockserver的期望
# ./api-mock/expectations_init.json
[
{
"httpRequest": {
"method": "GET",
"path": "/token",
"secure": true
},
"httpResponse": {
"statusCode": 200,
"body": "Hello World - secure"
}
},
{
"httpRequest": {
"method": "GET",
"path": "/token",
"secure": false
},
"httpResponse": {
"statusCode": 200,
"body": "Hello World"
}
}
]
我的项目结构
stackoverflow
- ./api-mock
- expectations_init.json
- docker-compose.yml
要运行这个最小的示例,只需运行
docker-compose up -d
查看mockserver的仪表板
localhost:1080/mockserver/dashboard
我所期望的是:
docker exec stackoverflow_busybox_1 curl -k https://oauth2.googleapis.com/token
# curl: (7) Failed connect to oauth2.googleapis.com:443; Connection refused
取而代之的是:
docker exec stackoverflow_busybox_1 curl -k https://oauth2.googleapis.com:1080/token
# Hello World - secure
这里也是一样,希望能正常工作:
docker exec stackoverflow_busybox_1 curl -k http://oauth2.googleapis.com/token
# curl: (7) Failed connect to oauth2.googleapis.com:80; Connection refused
取而代之的是:
docker exec stackoverflow_busybox_1 curl -k http://oauth2.googleapis.com:1080/token
# Hello World
为了在不传递端口的情况下获得响应,我错过了什么配置,因为我无法控制供应商代码调用的URL。我在mockserver的文档中找不到任何关于这个用例的提示来实现这一点。也许这是docker/docker-compose的问题?
诚挚的问候
发布于 2020-10-02 19:47:18
问题不在于docker-compose,而在于mockserver镜像。因为它是在没有根权限的情况下执行的,所以它不能使用端口80,但它总是在端口1080上启动(有关更多信息,请查看https://www.mock-server.com/where/docker.html#run_docker_container )。
您可以通过使用以下命令启动docker镜像来更改端口:
docker run --rm --name mockserver mockserver/mockserver -serverPort 1090
这将允许您在端口1090上运行映像(这不是您想要的),或者您可以强制容器以root身份运行,并将端口更改为80。例如:
docker run --user root --rm --name mockserver mockserver/mockserver -serverPort 80
无论如何,可能有很好的理由不使用root。我从来没有用过这个软件,所以我真的不能说。
然后,您可以轻松地以docker-compose格式移植该命令
发布于 2020-10-06 18:44:01
这种设置效果很好,特别是在使用mockserver转发请求时:
nginx-vhosts:
server {
listen 80 default;
location / {
return 418;
}
}
server {
listen 443 ssl;
ssl_certificate /etc/ssl/certs/local.pub.pem;
ssl_certificate_key /etc/ssl/private/local.key.pem;
ssl_session_cache shared:SSL:16m;
ssl_session_timeout 10m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers RC4:HIGH:!aNULL:!MD5:!kEDH;
ssl_prefer_server_ciphers on;
location / {
return 418;
}
}
docker-compose.yml
version: '3.8'
services:
nginx:
build:
context: .
dockerfile: docker/nginx/Dockerfile
networks:
- our-network
busybox:
image: yauritux/busybox-curl:latest
command: tail -f /dev/null
networks:
- our-network
api-mock: *api-mock
image: mockserver/mockserver
user: root
command: -serverPort 80
networks:
our-network:
aliases:
- oauth2.googleapis.com
environment:
MOCKSERVER_INITIALIZATION_JSON_PATH: /api-mock/expectations_init.json
MOCKSERVER_WATCH_INITIALIZATION_JSON: 'true'
volumes:
- ./api-mock/:/api-mock
ports:
- 1080:80
api-mock-secure:
<<: *api-mock
command: -serverPort 443
ports:
- 1081:443
networks:
our-network:
expectations_init.json
[
{
"httpRequest": {
"method": "GET",
"path": "/token",
"headers": {
"Host": [ "oauth2.googleapis.com" ]
},
"secure": true
},
"httpResponse": {
"statusCode": 200,
"body": "Hello World - secure\n"
}
},
{
"httpRequest": {
"method": "GET",
"path": "/token",
"headers": {
"Host": [ "oauth2.googleapis.com" ]
},
"secure": false
},
"httpResponse": {
"statusCode": 200,
"body": "Hello World\n"
}
},
{
"httpRequest": {
"secure": false
},
"httpForward": {
"host": "nginx",
"port": 80,
"scheme": "HTTP"
}
},
{
"httpRequest": {
"secure": true
},
"httpForward": {
"host": "nginx",
"port": 443,
"scheme": "HTTPS"
}
}
]
感谢Stefano
https://stackoverflow.com/questions/64169990
复制相似问题