Git Product home page Git Product logo

testsecureapisample's Introduction

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.

testsecureapisample's People

Contributors

ardalis avatar imgbotapp avatar

Stargazers

Hailin Shu avatar  avatar  avatar Jeremy Knight avatar Pavel Kucherov avatar Andy Kohler avatar Cocoy avatar Dejan Milicic avatar Mohammad Helal avatar  avatar Jon Sagara avatar Jesus Santander avatar Shawn Vause avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.