2015-04-29 07:56:45 -04:00
package main
import (
2016-02-04 07:47:15 -05:00
"fmt"
2016-12-30 04:49:36 -05:00
"io/ioutil"
2015-04-29 07:56:45 -04:00
"net/http"
2016-11-02 12:43:29 -04:00
"runtime"
2015-06-17 17:57:32 -04:00
"strconv"
"strings"
2019-09-09 17:06:12 -04:00
"testing"
2015-04-29 07:56:45 -04:00
2015-06-17 17:57:32 -04:00
"github.com/docker/docker/api"
2018-05-04 17:15:00 -04:00
"github.com/docker/docker/api/types/versions"
2019-08-29 16:52:40 -04:00
"github.com/docker/docker/testutil/request"
2019-04-04 09:23:19 -04:00
"gotest.tools/assert"
2015-04-29 07:56:45 -04:00
)
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestAPIOptionsRoute ( c * testing . T ) {
2017-03-06 10:35:27 -05:00
resp , _ , err := request . Do ( "/" , request . Method ( http . MethodOptions ) )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , resp . StatusCode , http . StatusOK )
2015-04-29 07:56:45 -04:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestAPIGetEnabledCORS ( c * testing . T ) {
2017-03-06 10:35:27 -05:00
res , body , err := request . Get ( "/version" )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , res . StatusCode , http . StatusOK )
2015-07-23 07:24:14 -04:00
body . Close ( )
2015-04-29 07:56:45 -04:00
// TODO: @runcom incomplete tests, why old integration tests had this headers
// and here none of the headers below are in the response?
//c.Log(res.Header)
2019-04-04 09:23:19 -04:00
//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 07:56:45 -04:00
}
2015-05-19 12:28:50 -04:00
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestAPIClientVersionOldNotSupported ( c * testing . T ) {
2018-01-15 09:32:06 -05:00
if testEnv . OSType != runtime . GOOS {
2016-11-02 12:43:29 -04:00
c . Skip ( "Daemon platform doesn't match test platform" )
}
if api . MinVersion == api . DefaultVersion {
c . Skip ( "API MinVersion==DefaultVersion" )
}
2016-04-19 10:56:54 -04:00
v := strings . Split ( api . MinVersion , "." )
2015-06-17 17:57:32 -04:00
vMinInt , err := strconv . Atoi ( v [ 1 ] )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2015-06-17 17:57:32 -04:00
vMinInt --
v [ 1 ] = strconv . Itoa ( vMinInt )
version := strings . Join ( v , "." )
2017-03-06 10:35:27 -05:00
resp , body , err := request . Get ( "/v" + version + "/version" )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2016-12-30 04:49:36 -05:00
defer body . Close ( )
2019-04-04 09:23:19 -04:00
assert . Equal ( c , resp . StatusCode , http . StatusBadRequest )
2016-02-04 07:47:15 -05: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 , api . MinVersion )
2016-12-30 04:49:36 -05:00
content , err := ioutil . ReadAll ( body )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , strings . TrimSpace ( string ( content ) ) , expected )
2015-06-17 17:57:32 -04:00
}
2015-08-31 17:45:27 -04:00
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestAPIErrorJSON ( c * testing . T ) {
2017-03-06 10:35:27 -05:00
httpResp , body , err := request . Post ( "/containers/create" , request . JSONBody ( struct { } { } ) )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2018-05-04 17:15:00 -04:00
if versions . LessThan ( testEnv . DaemonAPIVersion ( ) , "1.32" ) {
2019-04-04 09:23:19 -04:00
assert . Equal ( c , httpResp . StatusCode , http . StatusInternalServerError )
2018-05-04 17:15:00 -04:00
} else {
2019-04-04 09:23:19 -04:00
assert . Equal ( c , httpResp . StatusCode , http . StatusBadRequest )
2018-05-04 17:15:00 -04:00
}
2019-04-04 09:23:19 -04: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 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , getErrorMessage ( c , b ) , "Config cannot be empty in order to create a container" )
2016-05-21 07:56:04 -04:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestAPIErrorPlainText ( c * testing . T ) {
2016-10-31 13:15:43 -04:00
// Windows requires API 1.25 or later. This test is validating a behaviour which was present
// in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors
testRequires ( c , DaemonIsLinux )
2017-03-06 10:35:27 -05:00
httpResp , body , err := request . Post ( "/v1.23/containers/create" , request . JSONBody ( struct { } { } ) )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2018-05-04 17:15:00 -04:00
if versions . LessThan ( testEnv . DaemonAPIVersion ( ) , "1.32" ) {
2019-04-04 09:23:19 -04:00
assert . Equal ( c , httpResp . StatusCode , http . StatusInternalServerError )
2018-05-04 17:15:00 -04:00
} else {
2019-04-04 09:23:19 -04:00
assert . Equal ( c , httpResp . StatusCode , http . StatusBadRequest )
2018-05-04 17:15:00 -04:00
}
2019-04-04 09:23:19 -04:00
assert . Assert ( c , strings . Contains ( httpResp . Header . Get ( "Content-Type" ) , "text/plain" ) )
2017-08-21 18:50:40 -04:00
b , err := request . ReadBody ( body )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , strings . TrimSpace ( string ( b ) ) , "Config cannot be empty in order to create a container" )
2016-05-21 07:56:04 -04:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestAPIErrorNotFoundJSON ( c * testing . T ) {
2016-05-21 07:56:04 -04:00
// 404 is a different code path to normal errors, so test separately
2017-03-06 10:35:27 -05:00
httpResp , body , err := request . Get ( "/notfound" , request . JSON )
2019-04-04 09:23:19 -04: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 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , getErrorMessage ( c , b ) , "page not found" )
2016-05-21 07:56:04 -04:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestAPIErrorNotFoundPlainText ( c * testing . T ) {
2017-03-06 10:35:27 -05:00
httpResp , body , err := request . Get ( "/v1.23/notfound" , request . JSON )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , httpResp . StatusCode , http . StatusNotFound )
assert . Assert ( c , strings . Contains ( httpResp . Header . Get ( "Content-Type" ) , "text/plain" ) )
2017-08-21 18:50:40 -04:00
b , err := request . ReadBody ( body )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
assert . Equal ( c , strings . TrimSpace ( string ( b ) ) , "page not found" )
2016-05-21 07:56:04 -04:00
}