github.com/yilecjw/go-swagger@v0.19.0/generator/client_test.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package generator 16 17 import ( 18 "io/ioutil" 19 "log" 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 const ( 28 defaultAPIPackage = "operations" 29 defaultClientPackage = "client" 30 defaultModelPackage = "models" 31 defaultServerPackage = "restapi" 32 33 basicFixture = "../fixtures/petstores/petstore.json" 34 ) 35 36 func testClientGenOpts() (g GenOpts) { 37 g.Target = "." 38 g.APIPackage = defaultAPIPackage 39 g.ModelPackage = defaultModelPackage 40 g.ServerPackage = defaultServerPackage 41 g.ClientPackage = defaultClientPackage 42 g.Principal = "" 43 g.DefaultScheme = "http" 44 g.IncludeModel = true 45 g.IncludeValidator = true 46 g.IncludeHandler = true 47 g.IncludeParameters = true 48 g.IncludeResponses = true 49 g.IncludeSupport = true 50 g.TemplateDir = "" 51 g.DumpData = false 52 g.IsClient = true 53 _ = g.EnsureDefaults() 54 return 55 } 56 57 func Test_GenerateClient(t *testing.T) { 58 log.SetOutput(ioutil.Discard) 59 60 // exercise safeguards 61 err := GenerateClient("test", []string{"model1"}, []string{"op1", "op2"}, nil) 62 assert.Error(t, err) 63 64 opts := testClientGenOpts() 65 opts.TemplateDir = "dir/nowhere" 66 err = GenerateClient("test", []string{"model1"}, []string{"op1", "op2"}, &opts) 67 assert.Error(t, err) 68 69 opts = testClientGenOpts() 70 opts.TemplateDir = "http://nowhere.com" 71 err = GenerateClient("test", []string{"model1"}, []string{"op1", "op2"}, &opts) 72 assert.Error(t, err) 73 74 opts = testClientGenOpts() 75 opts.Spec = "dir/nowhere.yaml" 76 err = GenerateClient("test", []string{"model1"}, []string{"op1", "op2"}, &opts) 77 assert.Error(t, err) 78 79 opts = testClientGenOpts() 80 opts.Spec = basicFixture 81 err = GenerateClient("test", []string{"model1"}, []string{}, &opts) 82 assert.Error(t, err) 83 84 opts = testClientGenOpts() 85 // bad content in spec (HTML...) 86 opts.Spec = "https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore.json" 87 err = GenerateClient("test", []string{}, []string{}, &opts) 88 assert.Error(t, err) 89 90 opts = testClientGenOpts() 91 // generate remote spec 92 opts.Spec = "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/yaml/petstore.yaml" 93 cwd, _ := os.Getwd() 94 tft, _ := ioutil.TempDir(cwd, "generated") 95 defer func() { 96 _ = os.RemoveAll(tft) 97 }() 98 opts.Target = tft 99 opts.IsClient = true 100 DefaultSectionOpts(&opts) 101 102 defer func() { 103 _ = os.RemoveAll(opts.Target) 104 }() 105 err = GenerateClient("test", []string{}, []string{}, &opts) 106 assert.NoError(t, err) 107 108 // just checks this does not fail 109 origStdout := os.Stdout 110 defer func() { 111 os.Stdout = origStdout 112 }() 113 tgt, _ := ioutil.TempDir(cwd, "dumped") 114 defer func() { 115 _ = os.RemoveAll(tgt) 116 }() 117 os.Stdout, _ = os.Create(filepath.Join(tgt, "stdout")) 118 opts.DumpData = true 119 err = GenerateClient("test", []string{}, []string{}, &opts) 120 assert.NoError(t, err) 121 _, err = os.Stat(filepath.Join(tgt, "stdout")) 122 assert.NoError(t, err) 123 } 124 125 func TestClient(t *testing.T) { 126 targetdir, err := ioutil.TempDir(os.TempDir(), "swagger_nogo") 127 if err != nil { 128 t.Fatalf("Failed to create a test target directory: %v", err) 129 } 130 log.SetOutput(ioutil.Discard) 131 defer func() { 132 _ = os.RemoveAll(targetdir) 133 log.SetOutput(os.Stdout) 134 }() 135 136 tests := []struct { 137 name string 138 template string 139 wantError bool 140 prepare func(opts *GenOpts) 141 }{ 142 { 143 name: "InvalidSpec", 144 wantError: true, 145 prepare: func(opts *GenOpts) { 146 opts.Spec = invalidSpecExample 147 opts.ValidateSpec = true 148 }, 149 }, 150 { 151 name: "BaseImportDisabled", 152 wantError: false, 153 }, 154 { 155 name: "Non_existing_contributor_template", 156 template: "NonExistingContributorTemplate", 157 wantError: true, 158 }, 159 { 160 name: "Existing_contributor", 161 template: "stratoscale", 162 wantError: false, 163 }, 164 } 165 166 for _, tt := range tests { 167 t.Run(tt.name, func(t *testing.T) { 168 opts := testClientGenOpts() 169 opts.Target = targetdir 170 opts.Spec = basicFixture 171 opts.LanguageOpts.BaseImportFunc = nil 172 opts.Template = tt.template 173 174 if tt.prepare != nil { 175 tt.prepare(&opts) 176 } 177 178 err := GenerateClient("foo", nil, nil, &opts) 179 if tt.wantError { 180 assert.Error(t, err) 181 } else { 182 assert.NoError(t, err) 183 } 184 }) 185 } 186 }