का उपयोग करने वाले यूनिट परीक्षण कोड को कैसे करें मैंने पाया है कि ओविन के पास यह शानदार Microsoft.Owin.Testing
लाइब्रेरी है जो आपको अपने वेब एप्लिकेशन को मेमोरी का परीक्षण करने देती है। हालांकि, मेरी साइट को उन संसाधनों तक पहुंचने से पहले प्रमाणीकरण की आवश्यकता है जिनमें जटिल लेखन परीक्षण कोड है।ओवेन कुकी ऑडिनेशन
क्या माइक्रोसॉफ्ट.ऑविन.स्टिंग का उपयोग करते समय प्रमाणीकरण के लिए "मॉक" प्रमाणीकरण का कोई सुविधाजनक तरीका है?
मैं एक आउट-ऑफ-प्रक्रिया एसटीएस हिट करने के लिए की जरूरत नहीं करने के लिए अपने इकाई परीक्षण चाहते हैं और मैं कोड है कि प्रोग्राम के रूप में एक में स्मृति एसटीएस (जैसे Thinktecture.IdentityServer.v3 के रूप में के खिलाफ में संकेत लिखने की ज़रूरत नहीं करना चाहते हैं)।
मेरे द्वारा आने वाला सबसे आसान समाधान यूनिट परीक्षणों के लिए प्रमाणीकरण कोड को अक्षम करना है, जिसमें से मैं प्रशंसक नहीं हूं।
मैं कुकी प्रमाणीकरण के साथ ओपनआईडी कनेक्ट का उपयोग कर रहा हूं। यहां एक निहित उदाहरण है। OpenId कनेक्ट के लिए कॉन्फ़िगरेशन स्ट्रिंग को वास्तविक सर्वर के लिए भरना होगा।
[Test]
public async void AccessAuthenthicatedResourceTest()
{
const string ClientId = "";
const string RedirectUri = "";
const string Authority = "";
TestServer server = TestServer.Create(
app =>
{
//Configure Open ID Connect With Cookie Authenthication
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = ClientId,
RedirectUri = RedirectUri,
Authority = Authority
});
// Requires Authentication
app.Use(
async (context, next) =>
{
var user = context.Authentication.User;
if (user == null
|| user.Identity == null
|| !user.Identity.IsAuthenticated)
{
context.Authentication.Challenge();
return;
}
await next();
});
app.Run(async context => await context.Response.WriteAsync("My Message"));
});
//Do or Bypass authenthication
HttpResponseMessage message = await server.CreateRequest("/").GetAsync();
Assert.AreEqual("My Message", await message.Content.ReadAsStringAsync());
}