public void Test_NoneJsonSite() { HttpJsonInterpretor target = new HttpJsonInterpretor(GetWebRequest("https://www.google.com.br/")); ////IInternetServiceListener object Response = target.GetObjectResponse(); Response.Should().BeNull(); }
private Match<AlbumDescriptor> GetFromMusicBrainzId(string mzid, bool NeedCover, CancellationToken iCancellationToken, MatchPrecision iMatchPrecision = MatchPrecision.Exact) { if (mzid == null) return null; HttpJsonInterpretor jsoncon = new HttpJsonInterpretor(MusicBrainzHttpCreator.ForReleaseIdSearch().SetValue(mzid).BuildRequest(_User, _Password)); dynamic res = jsoncon.GetObjectResponse(); if (res == null) return null; AlbumDescriptor alres = MusicBrainzJsonInterpretor.FromMusicBrainzRelease(res, NeedCover, iCancellationToken); if (alres == null) return null; return new Match<AlbumDescriptor>(alres, iMatchPrecision ); }
public void Test_Json_Response() { HttpJsonInterpretor target = new HttpJsonInterpretor(GetWebRequest("http://echo.jsontest.com/Name/David/Age/38/Nationality/French")); dynamic res = target.GetObjectResponse(); object Response = res; Response.Should().NotBeNull(); string Name = res.Name; Name.Should().Be("David"); string Age = res.Age; Age.Should().Be("38"); string Nationality = res.Nationality; Nationality.Should().Be("French"); object NoneExistingParameter = res.NoneExistingParameter; NoneExistingParameter.Should().BeNull(); }
private IEnumerable<Match<AlbumDescriptor>> SearchFromCDQuery(ICDInfoHandler CDUnit, bool NeedCover, CancellationToken iCancellationToken) { if (CDUnit == null) yield break; if (!CDUnit.IsReady) yield break; string discid = CDUnit.IDs.MusicBrainzCDId; HttpJsonInterpretor hji = new HttpJsonInterpretor(MusicBrainzHttpCreator.ForCDIdSearch().SetValue(discid).BuildRequest(_User,_Password)); dynamic myres = hji.GetObjectResponse(this); if (myres == null) yield break; foreach (AlbumDescriptor ad in MusicBrainzJsonInterpretor.FromMusicBrainzReleaseResults(myres, discid, iCancellationToken, NeedCover)) { yield return new Match<AlbumDescriptor>(ad, MatchPrecision.Suspition); } }
public void Test_wrong_Link() { HttpJsonInterpretor target = new HttpJsonInterpretor(GetWebRequest("http://www.google.com/barbapapa")); IInternetServiceListener listener = Substitute.For<IInternetServiceListener>(); object Response = target.GetObjectResponse(listener); Response.Should().BeNull(); listener.Received().OnWebExeption(Arg.Any<Exception>()); }
public void Test_Json_Response_Error_Case_Throw_Web_Exception() { IHttpWebRequest WebRequest = Substitute.For<IHttpWebRequest>(); HttpJsonInterpretor target = new HttpJsonInterpretor(WebRequest); WebException we = new WebException(); WebRequest.GetResponse().Returns(_ => { throw we; }); IInternetServiceListener listener = Substitute.For<IInternetServiceListener>(); object Response = target.GetObjectResponse(listener); Response.Should().BeNull(); listener.Received().OnWebExeption(we); }
public void Test_Json_Response_Error_Case_Stream_Null() { IHttpWebRequest WebRequest = Substitute.For<IHttpWebRequest>(); HttpJsonInterpretor target = new HttpJsonInterpretor(WebRequest); IHttpWebResponse WebResponse = Substitute.For<IHttpWebResponse>(); WebRequest.GetResponse().Returns(WebResponse); WebResponse.StatusCode.Returns(HttpStatusCode.OK); WebResponse.GetResponseStream().Returns((Stream)null); IInternetServiceListener listener = Substitute.For<IInternetServiceListener>(); object Response = target.GetObjectResponse(listener); Response.Should().BeNull(); listener.Received().OnUnExpectedUnreadableResult(); }
public void Test_Json_Response_Error_Case() { IHttpWebRequest WebRequest = Substitute.For<IHttpWebRequest>(); HttpJsonInterpretor target = new HttpJsonInterpretor(WebRequest); IHttpWebResponse WebResponse = Substitute.For<IHttpWebResponse>(); WebRequest.GetResponse().Returns(WebResponse); WebResponse.StatusCode.Returns(HttpStatusCode.Forbidden); IInternetServiceListener listener = Substitute.For<IInternetServiceListener>(); object Response = target.GetObjectResponse(listener); Response.Should().BeNull(); listener.Received().OnStatusCodeKO(HttpStatusCode.Forbidden); }
public void Test_Musicbrainz_Link() { HttpJsonInterpretor target = new HttpJsonInterpretor(GetWebRequest("http://musicbrainz.org/ws/2/release/59211ea4-ffd2-4ad9-9a4e-941d3148024a?inc=artist-credits+labels+discids+recordings&fmt=json")); IInternetServiceListener listener = Substitute.For<IInternetServiceListener>(); object Response = target.GetObjectResponse(listener); //Response.Should().BeNull(); listener.Received().OnWebExeption(Arg.Any<Exception>()); }
private IEnumerable<Match<AlbumDescriptor>> SearchFromDiscQuery(IAlbumDescriptor AlbumDescriptor, bool NeedCover, CancellationToken iCancellationToken) { string mzid = AlbumDescriptor.IDs.MusicBrainzID; if (!string.IsNullOrEmpty(mzid)) { Match<AlbumDescriptor> res = GetFromMusicBrainzId(mzid, NeedCover, iCancellationToken); if (res != null) yield return res; yield break; } HttpJsonInterpretor jsonconv = new HttpJsonInterpretor(MusicBrainzHttpCreator.ForReleaseSearch().SetArtist(AlbumDescriptor.Artist).SetDiscName(AlbumDescriptor.Name).BuildRequest(_User, _Password)); dynamic dynamicres = jsonconv.GetObjectResponse(); if (iCancellationToken.IsCancellationRequested) yield break; if ((dynamicres == null) || (dynamicres.count == 0)) yield break; List<dynamic> Releases = dynamicres.releases; if (Releases == null) yield break; foreach (dynamic rel in Releases) { if (iCancellationToken.IsCancellationRequested) break; Match<AlbumDescriptor> res = GetFromMusicBrainzId(rel.id, NeedCover, iCancellationToken, MatchPrecision.Suspition); if (res != null) yield return res; } }
public IEnumerable<Match<AlbumDescriptor>> Search(IWebQuery query, CancellationToken iCancellationToken) { if (_Activated == false) yield break; if ( (query==null) || (query.Type!=QueryType.FromAlbumInfo) || (_AuthManager==null)) yield break; IAlbumDescriptor lookingFor = query.AlbumDescriptor; if (lookingFor == null) yield break; Discogs2HttpRequestCreator src = new Discogs2HttpRequestCreator(_UA, _AuthManager, _TOut); HttpJsonInterpretor hji = new HttpJsonInterpretor(src.GetSearchRequest(lookingFor)); dynamic myres = hji.GetObjectResponse(this); if (myres == null) yield break; int Sizemax = myres.results.Count; if (Sizemax == 0) yield break; int FoundItem = 0; InternetServiceListener isl = new InternetServiceListener(); bool needcovers = query.NeedCoverArt; if (iCancellationToken.IsCancellationRequested) yield break; for (int i = 0; i < Sizemax; i++) { Thread.Sleep(1000); if (iCancellationToken.IsCancellationRequested) yield break; //hji = new HttpJsonInterpretor(src.FromUrl(myres.results[i].resource_url)); hji = new HttpJsonInterpretor(InternetProvider.InternetHelper.CreateAuthentified((string)myres.results[i].resource_url, Context.UserAgent, _AuthManager)); dynamic albumresponse = hji.GetObjectResponse(isl); if (albumresponse == null) continue; AlbumDescriptor res = AlbumDescriptor.FromDiscogs(albumresponse, needcovers, _AuthManager, Context, iCancellationToken); if (res != null) { FoundItem++; yield return new Match<AlbumDescriptor>(res, MatchPrecision.Suspition); if (FoundItem == query.MaxResult) break; } if (iCancellationToken.IsCancellationRequested) yield break; } if (!isl.IsOK) { FireEvent(InternetFailed.PartialResult(isl.Failures)); } }