Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FIT-Connect-SDK - .NET
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FIT-Connect
FIT-Connect-SDK - .NET
Commits
e1f3ac79
Commit
e1f3ac79
authored
2 years ago
by
Klaus Fischer
Browse files
Options
Downloads
Patches
Plain Diff
Added unit test to ensure default header contains User-Agent
parent
87d14487
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!29
830 Version Header
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
BasicUnitTest/RestCallServiceTest.cs
+10
-6
10 additions, 6 deletions
BasicUnitTest/RestCallServiceTest.cs
FitConnect/Services/RestCallService.cs
+8
-2
8 additions, 2 deletions
FitConnect/Services/RestCallService.cs
with
18 additions
and
8 deletions
BasicUnitTest/RestCallServiceTest.cs
+
10
−
6
View file @
e1f3ac79
using
System.Net.Http
;
using
FitConnect.Services
;
using
FluentAssertions
;
using
Microsoft.Extensions.Logging
;
using
NUnit.Framework
;
namespace
BasicUnitTest
;
internal
class
DummyRestService
:
RestCallService
{
public
DummyRestService
(
string
baseUrl
)
:
base
(
baseUrl
,
null
)
{
public
DummyRestService
(
)
:
base
(
"https://www.google.com"
,
null
)
{
}
public
HttpClient
GetClient
()
=>
CreateClient
();
}
public
class
RestCallServiceTest
{
[
Test
]
public
void
RestCallShouldContainVersionHeader
()
{
var
restCallService
=
new
DummyRestService
(
""
);
var
restCallService
=
new
DummyRestService
();
var
client
=
restCallService
.
GetClient
();
client
.
DefaultRequestHeaders
.
Contains
(
"User-Agent"
).
Should
().
BeTrue
();
client
.
DefaultRequestHeaders
.
GetValues
(
"User-Agent"
).
Should
()
.
ContainMatch
(
"FITConnectDotNetSDK*"
);
}
}
This diff is collapsed.
Click to expand it.
FitConnect/Services/RestCallService.cs
+
8
−
2
View file @
e1f3ac79
...
...
@@ -31,6 +31,7 @@ internal class RestCallService : IRestCallService {
};
var
client
=
new
HttpClient
(
clientHandler
);
client
.
DefaultRequestHeaders
.
Add
(
"Accept"
,
"application/json"
);
client
.
DefaultRequestHeaders
.
Add
(
"User-Agent"
,
GetVersionHeader
());
if
(
AccessToken
!=
null
)
client
.
DefaultRequestHeaders
.
Add
(
"Authorization"
,
$"Bearer
{
AccessToken
}
"
);
return
client
;
...
...
@@ -54,11 +55,15 @@ internal class RestCallService : IRestCallService {
accept
);
}
private
static
string
?
_versionHeader
;
internal
static
string
GetVersionHeader
()
{
if
(
_versionHeader
!=
null
)
return
_versionHeader
;
var
version
=
ProjectSpecification
.
PackageVersion
;
var
commit
=
ProjectSpecification
.
CommitId
;
var
osVersion
=
Environment
.
OSVersion
.
Platform
.
ToString
()
+
Environment
.
OSVersion
.
Version
;
return
$"FITConnectDotNetSDK/
{
version
}
(commit:
{
commit
}
; os:
{
osVersion
}
)"
;
_versionHeader
=
$"FITConnectDotNetSDK/
{
version
}
(commit:
{
commit
}
; os:
{
osVersion
}
)"
;
return
_versionHeader
;
}
protected
async
Task
<
string
>
RestCallForString
(
Uri
requestUri
,
HttpMethod
method
,
...
...
@@ -83,7 +88,8 @@ internal class RestCallService : IRestCallService {
_logger
?.
LogTrace
(
"Body: {Body}"
,
body
);
}
request
.
Headers
.
Add
(
"User-Agent"
,
GetVersionHeader
());
#warning moved to CreateClient
// request.Headers.Add("User-Agent", GetVersionHeader());
var
response
=
await
client
.
SendAsync
(
request
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment