Skip to content
A sample showing how to test a secure API endpoint using xunit, identityserver4, and environment variables
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
IdentityServerHost Adding health check and test Jun 26, 2019
SecureAPITests Updating dependencies Jun 26, 2019
.gitignore Adding health check and test Jun 26, 2019
IdentityServerAndApi.sln Initial version Jun 26, 2019
LICENSE
README.md adding notes Jun 26, 2019
RunAndTest.bat adding run and build batch file Jun 26, 2019
azure-pipelines.yml Update azure-pipelines.yml for Azure Pipelines Jun 26, 2019

README.md

TestSecureApiSample

Build Status

A sample showing how to test a secure API endpoint using xunit, identityserver4, and environment variables.

Goals

  • Demonstrate how to test a token-secured API endpoint running live (perhaps in a container) using xUnit
  • Demonstrate how to dynamically specify the API endpoint's URL using environment variables read by xUnit

Original Sample

Original IdentityServerHost project forked from here: https://github.com/brockallen/IdentityServerAndApi and only modified slightly.

Expected Output using Environment Variables for Configuration

Microsoft Windows [Version 10.0.17763.557]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\dev\Scratch\IdentityServerAndApi\SecureAPITests>SET ApiBaseUrl=http://google.com

C:\dev\Scratch\IdentityServerAndApi\SecureAPITests>dotnet test
Test run for C:\dev\Scratch\IdentityServerAndApi\SecureAPITests\bin\Debug\netcoreapp3.0\SecureAPITests.dll(.NETCoreApp,Version=v3.0)
Microsoft (R) Test Execution Command Line Tool Version 16.0.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:00.61]     SecureAPITests.UnitTest1.HitApiEndpoint [FAIL]
Failed   SecureAPITests.UnitTest1.HitApiEndpoint
Error Message:
 Assert.True() Failure
Expected: True
Actual:   False
Stack Trace:
   at SecureAPITests.UnitTest1.HitApiEndpoint() in C:\dev\Scratch\IdentityServerAndApi\SecureAPITests\UnitTest1.cs:line 92
--- End of stack trace from previous location where exception was thrown ---

Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 1.0766 Seconds

C:\dev\Scratch\IdentityServerAndApi\SecureAPITests>SET ApiBaseUrl=http://localhost:5000

C:\dev\Scratch\IdentityServerAndApi\SecureAPITests>dotnet test
Test run for C:\dev\Scratch\IdentityServerAndApi\SecureAPITests\bin\Debug\netcoreapp3.0\SecureAPITests.dll(.NETCoreApp,Version=v3.0)
Microsoft (R) Test Execution Command Line Tool Version 16.0.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

Total tests: 2. Passed: 2. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 0.9730 Seconds

C:\dev\Scratch\IdentityServerAndApi\SecureAPITests>

Notes

The app should be running on port 3611 using the checked-in launchSettings.json file. If not, you should adjust it to do so.

If ctrl-F5 in Visual Studio to run the API (IdentityServerHost project) and then when you run the tests it just dies with an "exited with -1" try running both the web server and the tests from two separate command lines, instead.

You can’t perform that action at this time.