2015-04-29 13:56:45 +02:00
package main
import (
2023-07-14 18:02:38 +00:00
"context"
2016-02-04 13:47:15 +01:00
"fmt"
2015-04-29 13:56:45 +02:00
"net/http"
2016-11-02 09:43:29 -07:00
"runtime"
2015-06-17 23:57:32 +02:00
"strconv"
"strings"
2019-09-09 21:06:12 +00:00
"testing"
2015-04-29 13:56:45 +02:00
2023-10-25 14:43:59 +02:00
"github.com/docker/docker/runconfig"
2023-07-14 18:02:38 +00:00
"github.com/docker/docker/testutil"
2019-08-29 15:52:40 -05:00
"github.com/docker/docker/testutil/request"
2020-02-07 14:39:24 +01:00
"gotest.tools/v3/assert"
2015-04-29 13:56:45 +02:00
)
2022-06-16 23:32:10 +02:00
type DockerAPISuite struct {
ds * DockerSuite
}
2023-07-14 18:02:38 +00:00
func ( s * DockerAPISuite ) TearDownTest ( ctx context . Context , c * testing . T ) {
s . ds . TearDownTest ( ctx , c )
2022-06-16 23:32:10 +02:00
}
func ( s * DockerAPISuite ) OnTimeout ( c * testing . T ) {
s . ds . OnTimeout ( c )
}
func ( s * DockerAPISuite ) TestAPIOptionsRoute ( c * testing . T ) {
2023-07-14 18:02:38 +00:00
resp , _ , err := request . Do ( testutil . GetContext ( c ) , "/" , request . Method ( http . MethodOptions ) )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
assert . Equal ( c , resp . StatusCode , http . StatusOK )
2015-04-29 13:56:45 +02:00
}
2022-06-16 23:32:10 +02:00
func ( s * DockerAPISuite ) TestAPIGetEnabledCORS ( c * testing . T ) {
2023-07-14 18:02:38 +00:00
res , body , err := request . Get ( testutil . GetContext ( c ) , "/version" )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
assert . Equal ( c , res . StatusCode , http . StatusOK )
2015-07-23 13:24:14 +02:00
body . Close ( )
2015-04-29 13:56:45 +02:00
// TODO: @runcom incomplete tests, why old integration tests had this headers
// and here none of the headers below are in the response?
2023-06-14 11:46:00 +02:00
// c.Log(res.Header)
// assert.Equal(c, res.Header.Get("Access-Control-Allow-Origin"), "*")
// assert.Equal(c, res.Header.Get("Access-Control-Allow-Headers"), "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
2015-04-29 13:56:45 +02:00
}
2015-05-19 09:28:50 -07:00
2022-06-16 23:32:10 +02:00
func ( s * DockerAPISuite ) TestAPIClientVersionOldNotSupported ( c * testing . T ) {
2023-06-14 11:46:00 +02:00
if testEnv . DaemonInfo . OSType != runtime . GOOS {
2016-11-02 09:43:29 -07:00
c . Skip ( "Daemon platform doesn't match test platform" )
}
2023-12-04 22:58:47 +01:00
major , minor , _ := strings . Cut ( testEnv . DaemonVersion . MinAPIVersion , "." )
vMinInt , err := strconv . Atoi ( minor )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
2015-06-17 23:57:32 +02:00
vMinInt --
2023-12-04 22:58:47 +01:00
version := fmt . Sprintf ( "%s.%d" , major , vMinInt )
2015-06-17 23:57:32 +02:00
2023-07-14 18:02:38 +00:00
resp , body , err := request . Get ( testutil . GetContext ( c ) , "/v" + version + "/version" )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
2016-12-30 10:49:36 +01:00
defer body . Close ( )
2019-04-04 15:23:19 +02:00
assert . Equal ( c , resp . StatusCode , http . StatusBadRequest )
2023-12-04 22:58:47 +01:00
expected := fmt . Sprintf ( "client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version" , version , testEnv . DaemonVersion . MinAPIVersion )
2024-01-21 16:08:40 +01:00
b , err := request . ReadBody ( body )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
2024-01-21 16:08:40 +01:00
assert . Equal ( c , getErrorMessage ( c , b ) , expected )
2015-06-17 23:57:32 +02:00
}
2015-08-31 14:45:27 -07:00
2022-06-16 23:32:10 +02:00
func ( s * DockerAPISuite ) TestAPIErrorJSON ( c * testing . T ) {
2023-07-14 18:02:38 +00:00
httpResp , body , err := request . Post ( testutil . GetContext ( c ) , "/containers/create" , request . JSONBody ( struct { } { } ) )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
2023-12-04 23:26:00 +01:00
assert . Equal ( c , httpResp . StatusCode , http . StatusBadRequest )
2019-04-04 15:23:19 +02:00
assert . Assert ( c , strings . Contains ( httpResp . Header . Get ( "Content-Type" ) , "application/json" ) )
2017-08-21 18:50:40 -04:00
b , err := request . ReadBody ( body )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
2023-10-25 14:43:59 +02:00
assert . Equal ( c , getErrorMessage ( c , b ) , runconfig . ErrEmptyConfig . Error ( ) )
2016-05-21 12:56:04 +01:00
}
2022-06-16 23:32:10 +02:00
func ( s * DockerAPISuite ) TestAPIErrorNotFoundJSON ( c * testing . T ) {
2016-05-21 12:56:04 +01:00
// 404 is a different code path to normal errors, so test separately
2023-07-14 18:02:38 +00:00
httpResp , body , err := request . Get ( testutil . GetContext ( c ) , "/notfound" , request . JSON )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
assert . Equal ( c , httpResp . StatusCode , http . StatusNotFound )
assert . Assert ( c , strings . Contains ( httpResp . Header . Get ( "Content-Type" ) , "application/json" ) )
2017-08-21 18:50:40 -04:00
b , err := request . ReadBody ( body )
2019-04-04 15:23:19 +02:00
assert . NilError ( c , err )
assert . Equal ( c , getErrorMessage ( c , b ) , "page not found" )
2016-05-21 12:56:04 +01:00
}